-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
executable file
·176 lines (152 loc) · 4.19 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
import fs from "fs-extra";
import path, {dirname} from "node:path";
import { fileURLToPath } from "node:url";
import { spawn } from "cross-spawn";
import { writeFileSync, existsSync } from "node:fs";
import chalk from "chalk";
import { createCommand } from "commander";
function readJson(file) {
return JSON.parse(fs.readFileSync(file).toString());
}
function currentDirName(importMetaUrl) {
return dirname(fileURLToPath(importMetaUrl));
}
const currentDir = currentDirName(import.meta.url);
const pkg = readJson(path.resolve(currentDir, "./package.json"));
const isUsingYarn = true;
// const isUsingYarn = (process.env.npm_config_user_agent || "").indexOf("yarn") === 0;
const cliPackageName = "lowcoder-cli";
const sdkPackageName = "lowcoder-sdk";
let verbose = false;
let registry;
createCommand(pkg.name)
.version(pkg.version)
.arguments("<project-directory>")
.usage(`${chalk.green("<project-directory>")} [Options]`)
.option("-t, --template", "template name", "typescript")
.option("-f, --force", "force create project, if target dir is not empty, will empty it")
.option("--verbose", "print more info")
.option("--registry [addr]", "npm registry")
.action((name, options) => {
verbose = options.verbose;
registry = options.registry;
return createProject(name, options);
})
.parse();
function writePackageJson(file, content) {
writeFileSync(file, JSON.stringify(content, null, 2));
}
function writeYarnFile() {
writeFileSync("yarn.lock", "");
}
async function isDirEmpty(dir) {
if (!existsSync(dir)) {
return true;
}
const files = await fs.promises.readdir(dir);
return files.length === 0;
}
async function install(dependencies) {
return new Promise((resolve, reject) => {
let cmd = "npm";
let args = ["install", "--no-audit", "--save", "--save-exact", "--loglevel", "error"];
if (isUsingYarn) {
cmd = "yarn";
args = ["add"];
}
if (registry) {
args.push("--registry", registry);
}
args.push(...dependencies);
const child = spawn(cmd, args, { stdio: "inherit" });
child.on("close", (code) => {
if (code !== 0) {
reject({
command: `${cmd} ${args.join(" ")}`,
});
return;
}
resolve();
});
});
}
function executeNodeScript({ cwd, args }, data, source) {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [...args, "-e", source, "--", JSON.stringify(data)], {
cwd,
stdio: "inherit",
});
child.on("close", (code) => {
if (code !== 0) {
reject({
command: `node ${args.join(" ")}`,
});
return;
}
resolve();
});
});
}
/**
* create lowcoder comps project
* 1. create dir
* 2. create package.json
* 3. install lowcoder-cli
* 4. run `lowcoder-cli init`
*/
async function createProject(projectName, options) {
const { template, force } = options;
const originalDirectory = process.cwd();
const root = path.resolve(originalDirectory, projectName);
const isRootEmpty = await isDirEmpty(root);
if (!isRootEmpty) {
if (force) {
fs.emptyDirSync(root);
} else {
console.log();
console.error(`${root} is not empty`);
process.exit(1);
}
}
const packageJsonFile = path.resolve(root, "package.json");
fs.ensureDirSync(root);
process.chdir(root);
const initialPackageJson = {
name: projectName,
version: "0.0.1",
type: "module",
license: "MIT",
};
// now we prepare the files
writePackageJson(packageJsonFile, initialPackageJson);
// without empty yarn file the setup will fail
writeYarnFile();
await install([
cliPackageName,
sdkPackageName,
"react@18",
"react-dom@18",
"@types/react@18",
"@types/react-dom@18",
"vite",
"prop-types",
"react-resize-detector",
"@observablehq/runtime",
"@observablehq/inspector",
"@observablehq/stdlib",
]);
await executeNodeScript(
{
cwd: process.cwd(),
args: ["--input-type=module"],
},
{ template, registry },
`
import init from '${cliPackageName}/actions/init.js';
init(JSON.parse(process.argv[1]));
`
);
process.chdir(originalDirectory);
console.log("Done.");
}