forked from temporalio/samples-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-shared-files.mjs
107 lines (90 loc) · 3.09 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
// Run with https://github.com/google/zx
const STORED_SAMPLES = new Set(require('./list-of-samples.json').samples);
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'];
const GITIGNORE_EXCLUDE = [
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'production',
'hello-world-js',
'protobufs',
];
const ESLINTRC_EXCLUDE = ['nextjs-ecommerce-oneclick', 'monorepo-folders', 'fetch-esm', 'hello-world-js', 'protobufs'];
const ESLINTIGNORE_EXCLUDE = ['production', 'hello-world-js', 'protobufs'];
const POST_CREATE_EXCLUDE = [
'timer-examples',
'query-subscriptions',
'nextjs-ecommerce-oneclick',
'monorepo-folders',
'hello-world-mtls',
'expense',
'production',
'patching-api',
'signals-queries',
'activities-cancellation-heartbeating',
];
const FILES = [
'.shared/tsconfig.json',
'.shared/.gitignore',
'.shared/.eslintrc.js',
'.shared/.post-create',
'.shared/.eslintignore',
'.shared/.npmrc',
'.shared/.nvmrc',
];
// 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);
}
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');
}
await copyAndAdd(sample, '.npmrc');
await copyAndAdd(sample, '.nvmrc');
}
console.log(' done.');
async function copyAndAdd(sample, file) {
await $`cp .shared/${file} ${sample}/`;
await $`git add ${sample}/${file}`;
}