-
Notifications
You must be signed in to change notification settings - Fork 122
/
copy-shared-files.mjs
207 lines (175 loc) · 5.48 KB
/
copy-shared-files.mjs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Run with https://github.com/google/zx
const { readFileSync } = require('fs');
const STORED_SAMPLES = new Set(require('./list-of-samples.json').samples);
const yaml = require('yaml');
const NON_SAMPLES = ['node_modules'];
const ADDITIONAL_SAMPLES = [];
// Some samples have different config files from those in .shared/
// that we don't want to overwrite
const TSCONFIG_EXCLUDE = [
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'fetch-esm',
'production',
'hello-world-js',
'food-delivery',
'nestjs-exchange-rates',
'empty',
'hello-world',
'scratchpad',
];
const GITIGNORE_EXCLUDE = [
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'production',
'hello-world-js',
'protobufs',
'food-delivery',
'nestjs-exchange-rates',
];
const ESLINTRC_EXCLUDE = [
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'fetch-esm',
'hello-world-js',
'protobufs',
'food-delivery',
'nestjs-exchange-rates',
];
const ESLINTIGNORE_EXCLUDE = [
'production',
'hello-world-js',
'protobufs',
'activities-examples',
'food-delivery',
'nestjs-exchange-rates',
];
const POST_CREATE_EXCLUDE = [
'schedules',
'timer-examples',
'query-subscriptions',
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'hello-world-mtls',
'expense',
'production',
'patching-api',
'signals-queries',
'activities-cancellation-heartbeating',
'nestjs-exchange-rates',
'food-delivery',
'search-attributes',
'worker-versioning',
'empty',
'scratchpad',
];
const PRETTIERRC_EXCLUDE = ['food-delivery'];
const PRETTIERIGNORE_EXCLUDE = [
'food-delivery',
'monorepo-folders',
'nextjs-ecommerce-oneclick',
'protobufs',
'nestjs-exchange-rates',
];
const NPMRC_EXCLUDE = ['food-delivery'];
const FILES = [
'.shared/tsconfig.json',
'.shared/.gitignore',
'.shared/.eslintrc.js',
'.shared/.post-create',
'.shared/.eslintignore',
'.shared/.nvmrc',
'.shared/.npmrc',
'.shared/.prettierrc',
'.shared/.prettierignore',
];
// By default, zx logs all commands spawned
$.verbose = false;
let numSharedFilesChanged = 0;
for (let i = 0; i < FILES.length; i++) {
const checkForFiles = await $`git diff --shortstat ${FILES[i]}`;
if (checkForFiles.stdout) {
++numSharedFilesChanged;
}
}
const dirents = await fs.readdir('.', { withFileTypes: true });
const samples = dirents
.filter((dirent) => dirent.isDirectory() && !NON_SAMPLES.includes(dirent.name) && dirent.name[0] !== '.')
.map(({ name }) => name)
.concat(ADDITIONAL_SAMPLES);
const hasNewSamples = samples.find((sample) => !STORED_SAMPLES.has(sample));
await fs.writeFile('./.scripts/list-of-samples.json', JSON.stringify({ samples }, null, ' '));
if (numSharedFilesChanged === 0 && !hasNewSamples) {
process.exit(0);
}
await $`git add ${'./.scripts/list-of-samples.json'}`;
let [answer] = await question(
`Running pre-commit hook.
This will overwrite changes made to most config files in samples (like ${chalk.bold('hello-world/tsconfig.json')}).
Proceed? [Y/n] `
);
if ((answer ?? 'y').toUpperCase() !== 'Y') {
console.log(`To change config files, edit them in the ${chalk.bold('.shared/')} directory.\nAborting commit.`);
process.exit(1);
}
process.stdout.write('Copying config files from .shared/ to samples...');
for (const sample of samples) {
if (!TSCONFIG_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, 'tsconfig.json');
}
if (!GITIGNORE_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.gitignore');
}
if (!ESLINTRC_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.eslintrc.js');
}
if (!POST_CREATE_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.post-create');
}
if (!ESLINTIGNORE_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.eslintignore');
}
if (!NPMRC_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.npmrc');
}
if (!PRETTIERRC_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.prettierrc');
}
if (!PRETTIERIGNORE_EXCLUDE.includes(sample)) {
await copyAndAdd(sample, '.prettierignore');
}
await copyAndAdd(sample, '.nvmrc');
}
process.stdout.write('Updating GitHub workflows...');
const ciConfig = yaml.parseDocument(await fs.readFile('.github/workflows/ci.yml', 'utf8'));
const jobsNode = ciConfig.contents.items.find((i) => i.key.value === 'jobs');
const testNode = jobsNode.value.items.find((i) => i.key.value === 'test-individual');
const testProjectsNode = testNode.value.items
.find((i) => i.key.value === 'strategy')
.value.items.find((i) => i.key.value === 'matrix')
.value.items.find((i) => i.key.value === 'project');
const lintNode = jobsNode.value.items.find((i) => i.key.value === 'lint-individual');
const lintProjectsNode = lintNode.value.items
.find((i) => i.key.value === 'strategy')
.value.items.find((i) => i.key.value === 'matrix')
.value.items.find((i) => i.key.value === 'project');
testProjectsNode.value.items = [];
lintProjectsNode.value.items = [];
for (const sample of samples) {
// Don't use require, because it won't work with ESM samples
const packageJson = JSON.parse(readFileSync(`../${sample}/package.json`));
const hasTestScript = !!packageJson.scripts.test;
const hasLintScript = !!packageJson.scripts.lint;
if (hasTestScript) {
testProjectsNode.value.items.push(sample);
}
if (hasLintScript) {
lintProjectsNode.value.items.push(sample);
}
}
await fs.writeFile('.github/workflows/ci.yml', ciConfig.toString());
console.log(' done.');
async function copyAndAdd(sample, file) {
await $`cp .shared/${file} ${sample}/`;
await $`git add ${sample}/${file}`;
}