Add test for js1 build viewconfigs

Summary:
This diff adds a new `--test` option to `js1 build viewconfigs` which will only check that the configs have not changed instead of writing the new/updated files. This will allow us to run sandcastle checks on the view configs

I also improved the output of the script to give better feedback during normal runs including an additional message and a summary of generated files

Reviewed By: TheSavior

Differential Revision: D15372843

fbshipit-source-id: 4988fc2405cc03137b540817e08d4365cb31fc34
This commit is contained in:
Rick Hanlon 2019-05-20 02:49:16 -07:00 коммит произвёл Facebook Github Bot
Родитель 531f11f084
Коммит 3ccfbd6c39
3 изменённых файлов: 97 добавлений и 21 удалений

Просмотреть файл

@ -4,7 +4,6 @@
* This source code is licensed under the MIT license found in the * This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
* *
* @emails oncall+react_native
* @flow * @flow
* @format * @format
*/ */
@ -12,17 +11,26 @@
'use strict'; 'use strict';
const generate = require('./generate-view-configs'); const generate = require('./generate-view-configs');
const yargs = require('yargs');
const [fileList] = process.argv.slice(2); const yargv = yargs.strict().option('t', {
alias: 'test',
describe: 'Test the changes and do not write files',
requiresArg: false,
type: 'boolean',
});
const CURRENT_VIEW_CONFIG_SCHEMAS = ['SliderSchema.js']; const argv = yargv.argv;
const fileList = argv._[0].split('\n');
const CURRENT_VIEW_CONFIG_SCHEMAS = [''];
generate( generate(
fileList fileList.filter(fileName =>
.split('\n') CURRENT_VIEW_CONFIG_SCHEMAS.find(supportedFileName =>
.filter(fileName => fileName.endsWith(supportedFileName),
CURRENT_VIEW_CONFIG_SCHEMAS.find(supportedFileName =>
fileName.endsWith(supportedFileName),
),
), ),
),
// $FlowFixMe Type argv
argv.test,
); );

Просмотреть файл

@ -15,20 +15,54 @@ const RNParser = require('../src/generators/RNParser.js');
const path = require('path'); const path = require('path');
function generate(files: Array<string>): void { type Result = $ReadOnly<{|
files.forEach(filename => { libraryName: string,
success: boolean,
|}>;
function generateFilesWithResults(
files: Array<string>,
test: boolean,
): Array<Result> {
return files.reduce((aggregated, filename) => {
const schema = RNParser.parse(filename); const schema = RNParser.parse(filename);
if (schema && schema.modules) { if (schema && schema.modules) {
RNCodegen.generate( const libraryName = path.basename(filename).replace('Schema.js', '');
const success = RNCodegen.generate(
{ {
schema, schema,
libraryName,
outputDirectory: path.dirname(filename), outputDirectory: path.dirname(filename),
libraryName: path.basename(filename).replace('Schema.js', ''),
}, },
{generators: ['view-configs']}, {generators: ['view-configs'], test},
); );
aggregated.push({
libraryName,
success,
});
} }
}); return aggregated;
}, []);
}
function generate(files: Array<string>, test: boolean): void {
console.log(`${test ? 'Testing' : 'Generating'} view configs`);
const results = generateFilesWithResults(files, test);
const failed = results.filter(result => !result.success);
const totalCount = results.length;
console.log(`\n${test ? 'Tested' : 'Generated'} ${totalCount} view configs`);
if (failed.length) {
if (test === true) {
console.error(`${failed.length} configs changed`);
console.error("Please re-run 'js1 build viewconfigs'");
}
process.exit(1);
}
} }
module.exports = generate; module.exports = generate;

Просмотреть файл

@ -47,6 +47,7 @@ type Generators =
type Config = $ReadOnly<{| type Config = $ReadOnly<{|
generators: Array<Generators>, generators: Array<Generators>,
test?: boolean,
|}>; |}>;
const GENERATORS = { const GENERATORS = {
@ -61,18 +62,45 @@ const GENERATORS = {
'view-configs': [generateViewConfigJs.generate], 'view-configs': [generateViewConfigJs.generate],
}; };
function writeMapToFiles(map: Map<string, string>, outputDirectory: string) { function writeMapToFiles(map: Map<string, string>, outputDir: string) {
let success = true;
map.forEach((contents: string, fileName: string) => { map.forEach((contents: string, fileName: string) => {
const location = path.join(outputDirectory, fileName); try {
fs.writeFileSync(location, contents); const location = path.join(outputDir, fileName);
fs.writeFileSync(location, contents);
} catch (error) {
success = false;
console.error(`Failed to write ${fileName} to ${outputDir}`, error);
}
}); });
return success;
}
function checkFilesForChanges(
map: Map<string, string>,
outputDir: string,
): boolean {
let hasChanged = false;
map.forEach((contents: string, fileName: string) => {
const location = path.join(outputDir, fileName);
const currentContents = fs.readFileSync(location, 'utf8');
if (currentContents !== contents) {
console.error(`- ${fileName} has changed`);
hasChanged = true;
}
});
return !hasChanged;
} }
module.exports = { module.exports = {
generate( generate(
{libraryName, schema, outputDirectory}: Options, {libraryName, schema, outputDirectory}: Options,
{generators}: Config, {generators, test}: Config,
) { ): boolean {
schemaValidator.validate(schema); schemaValidator.validate(schema);
const generatedFiles = []; const generatedFiles = [];
@ -82,6 +110,12 @@ module.exports = {
} }
} }
writeMapToFiles(new Map([...generatedFiles]), outputDirectory); const filesToUpdate = new Map([...generatedFiles]);
if (test === true) {
return checkFilesForChanges(filesToUpdate, outputDirectory);
}
return writeMapToFiles(filesToUpdate, outputDirectory);
}, },
}; };