-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathcopy_template.ts
46 lines (41 loc) · 1.19 KB
/
copy_template.ts
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
import * as fs from 'fs-extra';
import * as path from 'path';
import * as util from 'util';
import { readFile, writeFile } from 'fs/promises';
/**
* Copies the specified template directory to a new package directory
*/
const { values } = util.parseArgs({
options: {
template: {
type: 'string',
},
name: {
type: 'string',
},
},
});
if (!values?.name || !values?.template) {
throw new Error(
'Specify a package template using --template=<string> and a new package name using --name=<string>. Valid template names are the directory names under ./templates'
);
}
const sourcePath = path.resolve(
new URL('.', import.meta.url).host,
'templates',
values.template as string
);
const destPath = path.resolve(
new URL('.', import.meta.url).host,
'packages',
values.name as string
);
await fs.copy(sourcePath, destPath);
// substitute in the library name into the new package.json file
const packageJsonPath = path.resolve(destPath, 'package.json');
const tokenizedPackageJson = await readFile(packageJsonPath, 'utf-8');
const newPackageJson = tokenizedPackageJson.replaceAll(
'{{libName}}',
values.name as string
);
await writeFile(packageJsonPath, newPackageJson);