This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (107 loc) · 3.64 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
const fs = require("fs");
const os = require("os");
const path = require("path");
const chalk = require("chalk");
const Commander = require("commander");
const packageJson = require("./package.json");
const validateNpmPackageName = require("validate-npm-package-name");
const mkdir = require("make-dir");
const cpy = require("cpy");
const promisify = require("util").promisify;
const fsStat = promisify(fs.stat);
const fsReaddir = promisify(fs.readdir);
const spawn = require("cross-spawn");
let projectPath = "";
new Commander.Command("create-hubs-app")
.version(packageJson.version)
.arguments("<project-path>")
.usage(`${chalk.green("<project-path>")} [options]`)
.action((arg) => {
projectPath = arg.trim();
})
.allowUnknownOption()
.parse(process.argv);
async function main() {
const resolvedProjectPath = path.resolve(projectPath);
const projectName = path.basename(resolvedProjectPath);
const { validForNewPackages, errors } = validateNpmPackageName(projectName);
if (!validForNewPackages) {
throw new Error(`Invalid project name.${(errors && errors.length > 0 && " " + errors[0]) || ""}`);
}
console.log(`Creating a new Hubs app in ${resolvedProjectPath}.`);
await createProjectDirectory(resolvedProjectPath);
await cpy(path.join(__dirname, "template"), resolvedProjectPath);
const initPackageJson = {
name: projectName,
version: "0.1.0",
scripts: {
login: "hubs login",
start: "hubs start",
deploy: "hubs deploy",
logout: "hubs logout"
}
};
fs.writeFileSync(
path.join(resolvedProjectPath, "package.json"),
JSON.stringify(initPackageJson, null, 2) + os.EOL
);
console.log("Installing packages. This might take a couple of minutes.");
await installNpmPackages(resolvedProjectPath, ["hubs-sdk"]);
console.log(chalk.green("Finished!"));
console.log(`Created ${projectName} at ${resolvedProjectPath}
Inside that directory you can run the following commands:
${chalk.blue("npm run login")}
Log into your Hubs Cloud server. You need to do this first.
${chalk.blue("npm start")}
Start the development server for your Hubs app.
${chalk.blue("npm run deploy")}
Build and deploy your Hubs app to your Hubs Cloud instance.
${chalk.blue("npm run logout")}
Log out of your Hubs Cloud server.
Check the documentation for more information on getting set up with Hubs Cloud.
https://hubs.mozilla.com/docs`);
}
main().catch((error) => {
console.error(chalk.red("Error creating Hubs app:"));
console.error(" " + chalk.red(error));
process.exit(1)
});
async function createProjectDirectory(projectPath) {
if (fs.existsSync(projectPath)) {
const stat = await fsStat(projectPath);
if (!stat.isDirectory()) {
throw new Error(`${projectPath} is not a directory.`);
}
let files = await fsReaddir(projectPath);
const ignoredFiles = [".DS_Store", "Thumbs.db"];
files = files.filter((file) => ignoredFiles.indexOf(file) === -1);
if (files.length !== 0) {
throw new Error(`${projectPath} is not an empty directory.`);
}
} else {
await mkdir(projectPath);
}
}
function installNpmPackages(cwd, packages) {
const prevCwd = process.cwd();
process.chdir(cwd);
const args = [
'install',
'--save',
'--save-exact',
'--loglevel',
'error',
].concat(packages);
return new Promise((resolve, reject) => {
const child = spawn("npm", args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject(new Error(`Error running npm install`));
} else {
resolve();
}
});
}).finally(() => {
process.chdir(prevCwd);
});
}