build(docs-infra): simplify update workflow for CLI-based docs examples boilerplate (#38992)
When updating the boilerplate for CLI-based docs examples, one needed to install dependencies inside the `aio/tools/examples/shared/boilerplate/cli/` directory, which resulted in a `node_modules/` directory and a `yarn.lock` file. These were not supposed to be part of the boilerplate, so they had to be manually removed after the boilerplate was updated. This commit simplifies the workflow by allowing boilerplate files to be ignored (both by git and the `example-boilerplate.js` script) via a `.gitignore` file. This way, it is no longer necessary to manually remove the unneeded directories/files. PR Close #38992
This commit is contained in:

committed by
Alex Rickabaugh

parent
37ed4bd9ff
commit
f6052a915d
@ -1,5 +1,6 @@
|
||||
const fs = require('fs-extra');
|
||||
const glob = require('glob');
|
||||
const ignore = require('ignore');
|
||||
const path = require('canonical-path');
|
||||
const shelljs = require('shelljs');
|
||||
const yargs = require('yargs');
|
||||
@ -23,6 +24,8 @@ class ExampleBoilerPlate {
|
||||
// Get all the examples folders, indicated by those that contain a `example-config.json` file
|
||||
const exampleFolders =
|
||||
this.getFoldersContaining(EXAMPLES_BASE_PATH, EXAMPLE_CONFIG_FILENAME, 'node_modules');
|
||||
const gitignore = ignore().add(fs.readFileSync(path.resolve(BOILERPLATE_BASE_PATH, '.gitignore'), 'utf8'));
|
||||
const isPathIgnored = absolutePath => gitignore.ignores(path.relative(BOILERPLATE_BASE_PATH, absolutePath));
|
||||
|
||||
if (!fs.existsSync(SHARED_NODE_MODULES_PATH)) {
|
||||
throw new Error(
|
||||
@ -48,22 +51,22 @@ class ExampleBoilerPlate {
|
||||
// boilerplate files first.
|
||||
// (Some of these files might be later overwritten by type-specific files.)
|
||||
if (boilerPlateType !== 'cli' && boilerPlateType !== 'systemjs') {
|
||||
this.copyDirectoryContents(BOILERPLATE_CLI_PATH, exampleFolder);
|
||||
this.copyDirectoryContents(BOILERPLATE_CLI_PATH, exampleFolder, isPathIgnored);
|
||||
}
|
||||
|
||||
// Copy the type-specific boilerplate files.
|
||||
this.copyDirectoryContents(boilerPlateBasePath, exampleFolder);
|
||||
this.copyDirectoryContents(boilerPlateBasePath, exampleFolder, isPathIgnored);
|
||||
|
||||
// Copy the common boilerplate files (unless explicitly not used).
|
||||
if (exampleConfig.useCommonBoilerplate !== false) {
|
||||
this.copyDirectoryContents(BOILERPLATE_COMMON_PATH, exampleFolder);
|
||||
this.copyDirectoryContents(BOILERPLATE_COMMON_PATH, exampleFolder, isPathIgnored);
|
||||
}
|
||||
|
||||
// Copy ViewEngine (pre-Ivy) specific files
|
||||
if (viewengine) {
|
||||
const veBoilerPlateType = boilerPlateType === 'systemjs' ? 'systemjs' : 'cli';
|
||||
const veBoilerPlateBasePath = path.resolve(BOILERPLATE_VIEWENGINE_PATH, veBoilerPlateType);
|
||||
this.copyDirectoryContents(veBoilerPlateBasePath, exampleFolder);
|
||||
this.copyDirectoryContents(veBoilerPlateBasePath, exampleFolder, isPathIgnored);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -89,25 +92,30 @@ class ExampleBoilerPlate {
|
||||
|
||||
loadJsonFile(filePath) { return fs.readJsonSync(filePath, {throws: false}) || {}; }
|
||||
|
||||
copyDirectoryContents(srcDir, dstDir) {
|
||||
copyDirectoryContents(srcDir, dstDir, isPathIgnored) {
|
||||
shelljs.ls('-Al', srcDir).forEach(stat => {
|
||||
const srcPath = path.resolve(srcDir, stat.name);
|
||||
const dstPath = path.resolve(dstDir, stat.name);
|
||||
|
||||
if (isPathIgnored(srcPath)) {
|
||||
// `srcPath` is ignored (e.g. by a `.gitignore` file): Ignore it.
|
||||
return;
|
||||
}
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
// `srcPath` is a directory: Recursively copy it to `dstDir`.
|
||||
shelljs.mkdir('-p', dstPath);
|
||||
return this.copyDirectoryContents(srcPath, dstPath);
|
||||
} else {
|
||||
// `srcPath` is a file: Copy it to `dstDir`.
|
||||
// (Also make the file non-writable to avoid accidental editing of boilerplate files).
|
||||
if (shelljs.test('-f', dstPath)) {
|
||||
// If the file already exists, ensure it is writable (so it can be overwritten).
|
||||
shelljs.chmod(666, dstPath);
|
||||
}
|
||||
shelljs.cp(srcPath, dstDir);
|
||||
shelljs.chmod(444, dstPath);
|
||||
return this.copyDirectoryContents(srcPath, dstPath, isPathIgnored);
|
||||
}
|
||||
|
||||
// `srcPath` is a file: Copy it to `dstDir`.
|
||||
// (Also make the file non-writable to avoid accidental editing of boilerplate files).
|
||||
if (shelljs.test('-f', dstPath)) {
|
||||
// If the file already exists, ensure it is writable (so it can be overwritten).
|
||||
shelljs.chmod(666, dstPath);
|
||||
}
|
||||
shelljs.cp(srcPath, dstDir);
|
||||
shelljs.chmod(444, dstPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user