forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
165 lines (149 loc) · 4.49 KB
/
sync.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright IBM Corp. 2018, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs-extra');
const path = require('path');
const prettier = require('prettier2'); //eslint-disable-line no-unused-vars
const lerna = require('../lerna.json'); //eslint-disable-line no-unused-vars
const packageJson = require('../package.json');
//eslint-disable-next-line no-unused-vars
const prettierOptions = {
...packageJson.prettier,
parser: 'markdown',
};
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
const REPO_URL_BASE =
'https://github.com/carbon-design-system/carbon/tree/master/packages';
// This is our default set of keywords to include in each `package.json` file
const DEFAULT_KEYWORDS = [
'ibm',
'carbon',
'carbon-design-system',
'components',
'react',
];
// We're going to use this in our `sortFields` method. The idea is that we want
// our `package.json` files to be ordered in the order given in this array. To
// accomplish this, we create an object where we can reference the value
// assigned to a field when sorting. By default, highest priority fields start
// with 1 and go up. Unknown fields are all given the same priority, which is
// just the length of the array + 1. When we use `sortFields` we are checking
// for the value from `packageJsonFields` and comparing it with the other value.
const packageJsonFields = [
'name',
'private',
'description',
'version',
'license',
'bin',
'main',
'module',
'repository',
'bugs',
'homepage',
'engines',
'files',
'keywords',
'publishConfig',
'scripts',
'resolutions',
'peerDependencies',
'dependencies',
'devDependencies',
'sideEffects',
'eyeglass',
'eslintConfig',
'prettier',
'babel',
'jest',
].reduce(
(acc, key, index) => ({
...acc,
[key]: index + 1,
}),
{}
);
const UNKNOWN_FIELD = Object.keys(packageJsonFields).length + 1;
function sortFields(a, b) {
const aValue = packageJsonFields[a] || UNKNOWN_FIELD;
const bValue = packageJsonFields[b] || UNKNOWN_FIELD;
return aValue - bValue;
}
async function sync() {
const packagePaths = await Promise.all(
(await fs.readdir(PACKAGES_DIR)).map(async (pkg) => {
const packageJsonPath = path.join(PACKAGES_DIR, pkg, 'package.json');
return {
basename: pkg,
filepath: packageJsonPath,
file: await fs.readJson(packageJsonPath),
packagePath: path.join(PACKAGES_DIR, pkg),
};
})
);
const packages = await Promise.all(
packagePaths.map(async ({ basename, filepath, file, ...rest }) => {
file.repository = `${REPO_URL_BASE}/${basename}`;
file.bugs = 'https://github.com/carbon-design-system/carbon/issues';
file.license = 'Apache-2.0';
file.publishConfig = {
access: 'public',
provenance: 'true',
};
if (Array.isArray(file.keywords)) {
const keywordsToAdd = DEFAULT_KEYWORDS.filter((keyword) => {
return file.keywords.indexOf(keyword) === -1;
});
if (keywordsToAdd.length > 0) {
file.keywords = [...file.keywords, ...keywordsToAdd];
}
} else {
file.keywords = DEFAULT_KEYWORDS;
}
// Construct our new packageJson file with sorted fields
const packageJson = Object.keys(file)
.sort(sortFields)
.reduce(
(acc, key) => ({
...acc,
[key]: file[key],
}),
{}
);
await fs.writeJson(filepath, packageJson, { spaces: 2 });
return {
...rest,
basename,
filepath,
packageJson,
};
})
);
// Sync `.npmignore` files
const defaultIgnorePatterns = [
'**/__mocks__/**',
'**/__tests__/**',
'**/examples/**',
'**/tasks/**',
];
await Promise.all(
//eslint-disable-next-line no-unused-vars
packages.map(async ({ packageJson, packagePath }) => {
const ignorePath = path.join(packagePath, '.npmignore');
const ignorePatterns = [...defaultIgnorePatterns];
if (await fs.pathExists(ignorePath)) {
const ignoreFile = await fs.readFile(ignorePath, 'utf8');
const localIgnorePatterns = ignoreFile.split('\n').filter((pattern) => {
return ignorePatterns.indexOf(pattern) === -1;
});
ignorePatterns.push(...localIgnorePatterns);
}
await fs.writeFile(ignorePath, ignorePatterns.join('\n'));
})
);
}
sync().catch((error) => console.error(error));