-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstandardize-pjson.js
More file actions
96 lines (81 loc) · 3.41 KB
/
standardize-pjson.js
File metadata and controls
96 lines (81 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const { readFileSync } = require('fs');
const { join } = require('path');
const { resolveConfig } = require('./sf-config');
const { semverIsLessThan } = require('./semver');
const PackageJson = require('./package-json');
const { determineProjectType } = require('./project-type');
const PLUGIN_FILES = ['/messages', '/oclif.manifest.json'];
const CORE_PLUGIN_FILES_BLOCK_LIST = ['/oclif.lock', '/npm-shrinkwrap.json'];
const JIT_PLUGIN_FILES = ['/messages', '/oclif.manifest.json', '/oclif.lock', '/npm-shrinkwrap.json'];
module.exports = (packageRoot = require('./package-path')) => {
const config = resolveConfig(packageRoot);
const pjson = new PackageJson(packageRoot);
const license = pjson.get('license');
if (config.license && license !== config.license) {
pjson.contents.license = config.license;
pjson.actions.push(`updating license to ${config.license} to match config`);
}
if (!config.license && license !== 'Apache-2.0') {
pjson.contents.license = 'Apache-2.0';
pjson.actions.push(`updating license to Apache-2.0. Add a 'license' to your '.sfdevrc' to skip this.`);
}
const type = determineProjectType(packageRoot);
if (type === 'jit-plugin') {
// ensure that jit plugins have the correct files
pjson.contents.files = [...new Set([...pjson.contents.files, ...JIT_PLUGIN_FILES])].sort();
}
if (type === 'core-plugin') {
// ensure that core plugins have the correct files and do not have any in the block list
pjson.contents.files = [
...new Set([...pjson.contents.files.filter((f) => !CORE_PLUGIN_FILES_BLOCK_LIST.includes(f)), ...PLUGIN_FILES]),
].sort();
}
// GENERATE SCRIPTS
const scriptList = Object.entries(config.scripts);
const wireitList = Object.entries(config.wireit);
if (scriptList.length > 0) {
const scriptsChanged = [];
const scripts = pjson.get('scripts');
// eslint-disable-next-line prefer-const
for (let [scriptName, scriptCommand] of scriptList) {
if (scripts[scriptName] !== scriptCommand) {
scripts[scriptName] = scriptCommand;
scriptsChanged.push(scriptName);
}
}
pjson.actions.push(`standardizing scripts: ${scriptsChanged.join(', ')}`);
if (wireitList.length > 0) {
const wireit = pjson.get('wireit');
for (const [scriptName, scriptCommand] of wireitList) {
if (wireit[scriptName] !== scriptCommand) {
wireit[scriptName] = scriptCommand;
scriptsChanged.push(scriptName);
}
}
}
}
try {
const tsconfig = readFileSync(join(packageRoot, 'tsconfig.json')).toString();
const engineVersion = '>=16.0.0';
// Don't control for non dev-config projects, or projects that don't specify an engine already.
if (
tsconfig.match(/"extends"\s*:\s*".*@salesforce\/dev-config/) &&
pjson.contents.engines &&
pjson.contents.engines.node &&
pjson.contents.engines.node !== engineVersion &&
semverIsLessThan(pjson.contents.engines.node.replace('>=', ''), engineVersion.replace('>=', ''))
) {
pjson.actions.push('updating node engine');
pjson.contents.engines.node = engineVersion;
}
} catch (err) {
// Don't control for non typescript projects.
}
pjson.write();
};