-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
160 lines (145 loc) · 4.02 KB
/
index.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
#!/usr/bin/env node
import { Command } from 'commander';
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
import spawnAsync from './spawnAsync.mjs';
const workingDirectory = process.cwd();
const packageJson = JSON.parse(
await fs.readFile(new URL('./package.json', import.meta.url))
);
let context = {
templatePackage: 'mono-repo-with-parcel-template',
appName: process.argv[2],
templatePackageFileName: ''
};
async function initContext() {
const tarballUrl = await spawnAsync(
`npm view ${context.templatePackage} dist.tarball`,
{
stdio: 'pipe'
}
);
context.templatePackageFileName = tarballUrl.split('-/')[1].trim();
}
async function downloadTemplateTarball() {
await spawnAsync(`npm pack ${context.templatePackage}`, {
stdio: 'pipe'
});
const exists = await fs.pathExists(context.templatePackageFileName);
if (!exists) {
throw new Error('Could not download package from NPM');
}
}
async function extractPackage() {
await spawnAsync(`tar -xf ${context.templatePackageFileName}`, {
stdio: 'pipe'
});
await fs.remove(context.templatePackageFileName);
await fs.copy(
path.join(workingDirectory, 'package'),
path.join(workingDirectory, context.appName)
);
}
async function correctPackageJsonFiles() {
const correctRootPackageJson = async () => {
const packgeJsonPath = path.join(
workingDirectory,
context.appName,
'package.json'
);
// "name", "version" and "private" are required to publish the package, but are
// not necessary to be present in the final package.json.
const {
_name,
_version,
_files,
private: _,
...rest
} = await fs.readJson(packgeJsonPath);
await fs.writeFile(packgeJsonPath, JSON.stringify(rest, null, 2));
};
const correctSubPackageJson = async () => {
const packgeJsonPath = path.join(
workingDirectory,
context.appName,
'packages/react-library/package.json'
);
const { _name, ...rest } = await fs.readJson(packgeJsonPath);
await fs.writeFile(
packgeJsonPath,
JSON.stringify(
{
name: `@${context.appName}/react-library`,
...rest
},
null,
2
)
);
};
await correctRootPackageJson();
await correctSubPackageJson();
}
async function installPackages() {
await spawnAsync('pnpm install', {
stdio: 'inherit',
cwd: path.join(workingDirectory, context.appName)
});
}
async function runBuild() {
await spawnAsync('pnpm build', {
stdio: 'inherit',
cwd: path.join(workingDirectory, context.appName)
});
}
async function checkPnpm() {
let exists;
try {
exists = await spawnAsync('pnpm --version', {
stdio: 'pipe'
});
} catch (e) {
exists = false;
}
if (!exists) {
console.log(
"This template uses PNPM as it's Package Manager, which you currently do not have installed.\r\n\n" +
'Check the installation guide for PNPM: https://pnpm.io/installation\r\n' +
'After installing PNPM, start a fresh shell to have access to the global pnpm CLI.'
);
process.exit(1);
}
}
(async () => {
try {
const program = new Command(packageJson.name)
.version(packageJson.version)
.arguments('[project-name]')
.usage(`${chalk.green('<project-name>')} [options]`)
.allowUnknownOption()
.action((projectName) => {
context.appName = projectName;
});
program.parse(process.argv);
if (!context.appName) {
console.log(`Please specify the project name:`);
console.log(` ${program.name()} ${chalk.green('<project-name>')}`);
process.exit(1);
}
await checkPnpm();
await initContext();
await downloadTemplateTarball();
await extractPackage();
await correctPackageJsonFiles();
await fs.remove(path.join(workingDirectory, 'package'));
console.log('Project created');
await installPackages();
await runBuild();
console.log('Done!');
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();