Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

const {
generateCode,
} = require('../generate-artifacts-executor/generateNativeCode');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

jest.mock('../codegen-utils', () => ({
getCodegen: () => ({
generate: ({outputDirectory}: {outputDirectory: string}) => {
const {writeFileSync} = require('node:fs');
const {join} = require('node:path');
writeFileSync(join(outputDirectory, 'TestSpec.h'), 'generated code');
},
}),
}));

let root: string;
let temp: string;
let output: string;

beforeEach(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'codegen-cleanup-test-'));
temp = path.join(root, 'temp');
output = path.join(root, 'output');
fs.mkdirSync(temp);
jest.spyOn(os, 'tmpdir').mockReturnValue(temp);
});

afterEach(() => {
jest.restoreAllMocks();
fs.rmSync(root, {recursive: true, force: true});
});

it('removes temporary artifacts after copying the generated files', () => {
generateCode(
output,
{
library: {config: {name: 'TestSpec', type: 'modules'}},
schema: {modules: {}},
},
false,
'ios',
);

expect(fs.readFileSync(path.join(output, 'TestSpec.h'), 'utf8')).toBe(
'generated code',
);
expect(fs.readdirSync(temp)).toEqual([]);
});

it('removes the temporary directory when code generation throws', () => {
expect(() =>
generateCode(
output,
{
library: {config: {name: 'TestSpec', type: 'invalid'}},
schema: {modules: {}},
},
false,
'ios',
),
).toThrow('Invalid library type. invalid');
expect(fs.readdirSync(temp)).toEqual([]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,35 @@ function generateCode(
}

const libraryName = schemaInfo.library.config.name;
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), libraryName));
const tmpOutputDir = path.join(tmpDir, 'out');
fs.mkdirSync(tmpOutputDir, {recursive: true});

codegenLog(`Generating Native Code for ${libraryName} - ${platform}`);
const useLocalIncludePaths = includesGeneratedCode;
generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
platform,
schemaInfo.schema,
tmpOutputDir,
libraryName,
'com.facebook.fbreact.specs',
schemaInfo.library.config.type,
useLocalIncludePaths,
const tmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'react-native-codegen-'),
);
try {
const tmpOutputDir = path.join(tmpDir, 'out');
fs.mkdirSync(tmpOutputDir, {recursive: true});

// Finally, copy artifacts to the final output directory.
const outputDir = forceOutputPath
? outputPath
: (reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath);
fs.mkdirSync(outputDir, {recursive: true});
cpSyncRecursiveIfChanged(tmpOutputDir, outputDir);
codegenLog(`Generated artifacts: ${outputDir}`);
codegenLog(`Generating Native Code for ${libraryName} - ${platform}`);
const useLocalIncludePaths = includesGeneratedCode;
generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
platform,
schemaInfo.schema,
tmpOutputDir,
libraryName,
'com.facebook.fbreact.specs',
schemaInfo.library.config.type,
useLocalIncludePaths,
);

// Finally, copy artifacts to the final output directory.
const outputDir = forceOutputPath
? outputPath
: (reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath);
fs.mkdirSync(outputDir, {recursive: true});
cpSyncRecursiveIfChanged(tmpOutputDir, outputDir);
codegenLog(`Generated artifacts: ${outputDir}`);
} finally {
fs.rmSync(tmpDir, {recursive: true, force: true});
}
}

function reactNativeCoreLibraryOutputPath(
Expand Down
Loading