build(docs-infra): enable linting for `ng-packages-installer` scripts (#41429)

This commit enables linting for the scripts in
`aio/tools/ng-packages-installer/`. It also makes the necessary changes
to the files to make linting pass.

PR Close #41429
This commit is contained in:
George Kalpakas 2021-04-05 19:05:54 +03:00 committed by Andrew Scott
parent d42019d412
commit a623bf4e70
4 changed files with 51 additions and 20 deletions

View File

@ -61,7 +61,7 @@
"docs-test": "node tools/transforms/test.js", "docs-test": "node tools/transforms/test.js",
"redirects-test": "node tests/deployment/unit/test", "redirects-test": "node tests/deployment/unit/test",
"firebase-utils-test": "node tools/firebase-test-utils/test", "firebase-utils-test": "node tools/firebase-test-utils/test",
"tools-lint": "tslint --config \"tools/tslint.json\" --project \"tools/firebase-test-utils\"", "tools-lint": "tslint --config \"tools/tslint.json\" --project \"tools/firebase-test-utils\" && eslint tools/ng-packages-installer",
"tools-test": "yarn docs-test && yarn boilerplate:test && jasmine tools/ng-packages-installer/index.spec.js && jasmine scripts/deploy-to-firebase.spec.js && yarn firebase-utils-test", "tools-test": "yarn docs-test && yarn boilerplate:test && jasmine tools/ng-packages-installer/index.spec.js && jasmine scripts/deploy-to-firebase.spec.js && yarn firebase-utils-test",
"preserve-and-sync": "yarn docs", "preserve-and-sync": "yarn docs",
"serve-and-sync": "run-p \"docs-watch --watch-only\" \"start {@}\" --", "serve-and-sync": "run-p \"docs-watch --watch-only\" \"start {@}\" --",

View File

@ -0,0 +1,22 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:jasmine/recommended',
],
env: {
es2020: true,
jasmine: true,
node: true,
},
plugins: [
'jasmine',
],
rules: {
'indent': ['error', 2],
'linebreak-style': ['error', 'unix'],
'max-len': ['error', 120],
'quotes': ['error', 'single'],
'semi': ['error', 'always'],
'jasmine/new-line-before-expect': ['off'],
},
};

View File

@ -162,7 +162,9 @@ class NgPackagesInstaller {
this._log(`Overriding dependency with peerDependency: ${key}: ${peerDepRange}`); this._log(`Overriding dependency with peerDependency: ${key}: ${peerDepRange}`);
dependencies[key] = peerDepRange; dependencies[key] = peerDepRange;
} else { } else {
this._log(`${devDependencies[key] ? 'Overriding' : 'Assigning'} devDependency with peerDependency: ${key}: ${peerDepRange}`); this._log(
`${devDependencies[key] ? 'Overriding' : 'Assigning'} devDependency with peerDependency: ` +
`${key}: ${peerDepRange}`);
devDependencies[key] = peerDepRange; devDependencies[key] = peerDepRange;
} }
}); });
@ -183,10 +185,11 @@ class NgPackagesInstaller {
shelljs.exec(DIST_PACKAGES_BUILD_CMD); shelljs.exec(DIST_PACKAGES_BUILD_CMD);
} else { } else {
this._warn([ this._warn([
'Automatically building the local Angular/Zone.js packages is currently not supported on Windows.', 'Automatically building the local Angular/angular-in-memory-web-api/zone.js packages is currently not ' +
`Please, ensure '${ANGULAR_DIST_PACKAGES_DIR}', ${ZONEJS_DIST_PACKAGES_DIR} and '${ANGULAR_MISC_DIST_PACKAGES}' exist and are up-to-date ` + 'supported on Windows.',
`(e.g. by running '${DIST_PACKAGES_BUILD_SCRIPT}' in Git Bash for Windows, Windows Subsystem for Linux or ` + `Please, ensure '${ANGULAR_DIST_PACKAGES_DIR}', '${ZONEJS_DIST_PACKAGES_DIR}' and ` +
'a Linux docker container or VM).', `'${ANGULAR_MISC_DIST_PACKAGES}' exist and are up-to-date (e.g. by running '${DIST_PACKAGES_BUILD_SCRIPT}' ` +
'in Git Bash for Windows, Windows Subsystem for Linux or a Linux docker container or VM).',
'', '',
'Proceeding anyway...', 'Proceeding anyway...',
].join('\n')); ].join('\n'));
@ -265,7 +268,7 @@ class NgPackagesInstaller {
...collectPackages(ANGULAR_MISC_DIST_PACKAGES), ...collectPackages(ANGULAR_MISC_DIST_PACKAGES),
}; };
this._log('Found the following Angular distributables:', Object.keys(packageConfigs).map(key => `\n - ${key}`)); this._log('Found the following Angular distributables:', ...Object.keys(packageConfigs).map(key => `\n - ${key}`));
return packageConfigs; return packageConfigs;
} }
@ -277,7 +280,7 @@ class NgPackagesInstaller {
/** /**
* Log a message if the `debug` property is set to true. * Log a message if the `debug` property is set to true.
* @param {...string[]} messages - The messages to be logged. * @param {string[]} messages - The messages to be logged.
*/ */
_log(...messages) { _log(...messages) {
if (this.debug) { if (this.debug) {
@ -339,7 +342,8 @@ class NgPackagesInstaller {
const parsed = lockfile.parse(lockfileContent); const parsed = lockfile.parse(lockfileContent);
if (parsed.type !== 'success') { if (parsed.type !== 'success') {
throw new Error(`[${NgPackagesInstaller.name}]: Error parsing lockfile '${lockfilePath}' (result type: ${parsed.type}).`); throw new Error(
`[${NgPackagesInstaller.name}]: Error parsing lockfile '${lockfilePath}' (result type: ${parsed.type}).`);
} }
return parsed.object; return parsed.object;
@ -362,7 +366,7 @@ class NgPackagesInstaller {
/** /**
* Log a warning message do draw user's attention. * Log a warning message do draw user's attention.
* @param {...string[]} messages - The messages to be logged. * @param {string[]} messages - The messages to be logged.
*/ */
_warn(...messages) { _warn(...messages) {
const lines = messages.join(' ').split('\n'); const lines = messages.join(' ').split('\n');
@ -400,6 +404,7 @@ function main() {
return new NgPackagesInstaller(projectDir, options); return new NgPackagesInstaller(projectDir, options);
}; };
/* eslint-disable max-len */
yargs yargs
.usage('$0 <cmd> [args]') .usage('$0 <cmd> [args]')
@ -421,6 +426,7 @@ function main() {
.strict() .strict()
.wrap(yargs.terminalWidth()) .wrap(yargs.terminalWidth())
.argv; .argv;
/* eslint-enable max-len */
} }
module.exports = NgPackagesInstaller; module.exports = NgPackagesInstaller;

View File

@ -46,7 +46,7 @@ describe('NgPackagesInstaller', () => {
fs.existsSync.and.returnValue(true); fs.existsSync.and.returnValue(true);
installer.checkDependencies(); installer.checkDependencies();
expect(fs.existsSync).toHaveBeenCalledWith(path.resolve(projectDir, 'node_modules/_local_.json')); expect(fs.existsSync).toHaveBeenCalledWith(path.resolve(projectDir, 'node_modules/_local_.json'));
expect(installer._printWarning).toHaveBeenCalled(); expect(installer._printWarning).toHaveBeenCalledWith();
}); });
}); });
@ -163,7 +163,7 @@ describe('NgPackagesInstaller', () => {
it('should not continue processing', () => { it('should not continue processing', () => {
installer.installLocalDependencies(); installer.installLocalDependencies();
expect(installer._checkLocalMarker).toHaveBeenCalled(); expect(installer._checkLocalMarker).toHaveBeenCalledWith();
expect(installer._getDistPackages).not.toHaveBeenCalled(); expect(installer._getDistPackages).not.toHaveBeenCalled();
}); });
@ -171,7 +171,7 @@ describe('NgPackagesInstaller', () => {
installer.force = true; installer.force = true;
installer.installLocalDependencies(); installer.installLocalDependencies();
expect(installer._checkLocalMarker).not.toHaveBeenCalled(); expect(installer._checkLocalMarker).not.toHaveBeenCalled();
expect(installer._getDistPackages).toHaveBeenCalled(); expect(installer._getDistPackages).toHaveBeenCalledWith();
}); });
}); });
@ -180,16 +180,17 @@ describe('NgPackagesInstaller', () => {
beforeEach(() => { beforeEach(() => {
log = []; log = [];
fs.writeFileSync.and.callFake((filePath, contents) => filePath === packageJsonPath && log.push(`writeFile: ${contents}`)); fs.writeFileSync.and.callFake((filePath, contents) =>
filePath === packageJsonPath && log.push(`writeFile: ${contents}`));
installer._installDeps.and.callFake((...args) => log.push(`installDeps: ${args.join(' ')}`)); installer._installDeps.and.callFake((...args) => log.push(`installDeps: ${args.join(' ')}`));
installer._checkLocalMarker.and.returnValue(false); installer._checkLocalMarker.and.returnValue(false);
installer.installLocalDependencies(); installer.installLocalDependencies();
}); });
it('should parse the lockfile and get the dist packages', () => { it('should parse the lockfile and get the dist packages', () => {
expect(installer._checkLocalMarker).toHaveBeenCalled(); expect(installer._checkLocalMarker).toHaveBeenCalledWith();
expect(installer._parseLockfile).toHaveBeenCalledWith(yarnLockPath); expect(installer._parseLockfile).toHaveBeenCalledWith(yarnLockPath);
expect(installer._getDistPackages).toHaveBeenCalled(); expect(installer._getDistPackages).toHaveBeenCalledWith();
}); });
it('should temporarily overwrite the package.json files of local Angular packages', () => { it('should temporarily overwrite the package.json files of local Angular packages', () => {
@ -260,7 +261,7 @@ describe('NgPackagesInstaller', () => {
it('should overwrite package.json, then install deps, then restore original package.json', () => { it('should overwrite package.json, then install deps, then restore original package.json', () => {
expect(log).toEqual([ expect(log).toEqual([
`writeFile: ${expectedModifiedPackageJson}`, `writeFile: ${expectedModifiedPackageJson}`,
`installDeps: --pure-lockfile --check-files`, 'installDeps: --pure-lockfile --check-files',
`writeFile: ${dummyPackageJson}` `writeFile: ${dummyPackageJson}`
]); ]);
}); });
@ -316,7 +317,8 @@ describe('NgPackagesInstaller', () => {
expect(shelljs.exec).not.toHaveBeenCalled(); expect(shelljs.exec).not.toHaveBeenCalled();
expect(warning).toContain( expect(warning).toContain(
'Automatically building the local Angular/Zone.js packages is currently not supported on Windows.'); 'Automatically building the local Angular/angular-in-memory-web-api/zone.js packages is currently not ' +
'supported on Windows.');
expect(warning).toContain('Git Bash for Windows'); expect(warning).toContain('Git Bash for Windows');
expect(warning).toContain('Windows Subsystem for Linux'); expect(warning).toContain('Windows Subsystem for Linux');
expect(warning).toContain('Linux docker container or VM'); expect(warning).toContain('Linux docker container or VM');
@ -340,7 +342,8 @@ describe('NgPackagesInstaller', () => {
expect(installer._buildDistPackages).toHaveBeenCalledTimes(1); expect(installer._buildDistPackages).toHaveBeenCalledTimes(1);
}); });
it('should not build the local packages by default', () => { it('should not build the local packages, if `buildPackages` is false', () => {
installer = new NgPackagesInstaller(projectDir, {buildPackages: false});
installer._getDistPackages(); installer._getDistPackages();
expect(installer._buildDistPackages).not.toHaveBeenCalled(); expect(installer._buildDistPackages).not.toHaveBeenCalled();
}); });