2022-12-06 20:03:34 +00:00
|
|
|
const path = require("path");
|
2022-12-06 21:24:44 +00:00
|
|
|
const fs = require("fs-extra");
|
2022-12-21 21:21:39 +00:00
|
|
|
const enquirer = require("enquirer");
|
2022-12-06 21:24:44 +00:00
|
|
|
const execSync = require("child_process").execSync;
|
|
|
|
|
|
|
|
const { Command } = require("commander");
|
|
|
|
const validateProjectName = require("validate-npm-package-name");
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
const DEFAULT_EXAMPLE = "light-chat";
|
|
|
|
|
2022-12-06 21:34:24 +00:00
|
|
|
const supportedExamplesDir = path.resolve(__dirname, "./examples");
|
2022-12-21 22:30:02 +00:00
|
|
|
const supportedExamples = readDirNames(supportedExamplesDir);
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-21 22:30:02 +00:00
|
|
|
const init = async (name, description, version) => {
|
2022-12-22 22:39:50 +00:00
|
|
|
let appName;
|
|
|
|
let template;
|
|
|
|
|
|
|
|
const options = new Command()
|
|
|
|
.name(name)
|
|
|
|
.description(description)
|
|
|
|
.version(version, "-v, --version", "output the version number")
|
|
|
|
.arguments(
|
|
|
|
"<project-directory>",
|
|
|
|
"Project directory to initialize Waku app"
|
|
|
|
)
|
|
|
|
.action((_appName) => {
|
|
|
|
appName = _appName;
|
|
|
|
})
|
|
|
|
.option(
|
|
|
|
"-t, --template <path-to-template>",
|
|
|
|
"specify a template for the created project or you can use the wizard."
|
|
|
|
)
|
|
|
|
.allowUnknownOption()
|
|
|
|
.parse()
|
|
|
|
.opts();
|
|
|
|
|
|
|
|
template = options.template;
|
|
|
|
|
|
|
|
if (!template) {
|
|
|
|
const templatePrompt = new enquirer.Select({
|
|
|
|
name: "template",
|
|
|
|
message: "Select template",
|
|
|
|
choices: buildChoices(supportedExamples),
|
|
|
|
});
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
try {
|
|
|
|
template = await templatePrompt.run();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed at selecting a template: ${e.message}`);
|
|
|
|
process.exit(1);
|
2022-12-06 20:03:34 +00:00
|
|
|
}
|
2022-12-22 22:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportedExamples[template]) {
|
|
|
|
const supportedExamplesMessage = Object.keys(supportedExamples).reduce(
|
|
|
|
(acc, v) => {
|
|
|
|
acc += `\t${v}\n`;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
console.error(`Unknown template: ${template}`);
|
|
|
|
console.error(
|
|
|
|
`We support only following templates:\n${supportedExamplesMessage}`
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
createApp(appName, template);
|
2022-12-06 20:03:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function createApp(name, template) {
|
2022-12-22 22:39:50 +00:00
|
|
|
const appRoot = path.resolve(name);
|
|
|
|
const appName = path.basename(appRoot);
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
const templateDir = path.resolve(supportedExamplesDir, template);
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
terminateIfAppExists(appName);
|
|
|
|
terminateIfProjectNameInvalid(appName);
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
console.log(`Initializing ${appName} from ${template} template.`);
|
2022-12-06 20:03:34 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
fs.ensureDirSync(appName);
|
|
|
|
fs.copySync(templateDir, appRoot);
|
2022-12-06 21:24:44 +00:00
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
runNpmInApp(appRoot);
|
|
|
|
runGitInit(appRoot);
|
2022-12-06 21:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runNpmInApp(root) {
|
2022-12-22 22:39:50 +00:00
|
|
|
const packageJsonPath = path.resolve(root, "package.json");
|
|
|
|
|
|
|
|
if (!fs.existsSync(packageJsonPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Installing npm packages.");
|
|
|
|
try {
|
|
|
|
execSync(`npm install --prefix ${root}`, { stdio: "ignore" });
|
|
|
|
console.log("Successfully installed npm dependencies.");
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Failed to install npm dependencies", e);
|
|
|
|
}
|
2022-12-06 21:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runGitInit(root) {
|
2022-12-22 22:39:50 +00:00
|
|
|
if (isInGitRepository()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Initiating git repository.");
|
|
|
|
try {
|
|
|
|
execSync(`git init ${root}`, { stdio: "ignore" });
|
|
|
|
console.log("Successfully initialized git repo.");
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Git repo not initialized", e);
|
|
|
|
}
|
2022-12-06 21:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isInGitRepository() {
|
2022-12-22 22:39:50 +00:00
|
|
|
try {
|
|
|
|
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-06 20:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function terminateIfProjectNameInvalid(name) {
|
2022-12-22 22:39:50 +00:00
|
|
|
const validationResult = validateProjectName(name);
|
|
|
|
|
|
|
|
if (!validationResult.validForNewPackages) {
|
|
|
|
console.error(
|
|
|
|
`Cannot create a project named ${name} because of npm naming restrictions:\n`
|
|
|
|
);
|
|
|
|
[
|
|
|
|
...(validationResult.errors || []),
|
|
|
|
...(validationResult.warnings || []),
|
|
|
|
].forEach((error) => console.error(` * ${error}`));
|
|
|
|
console.error("\nPlease choose a different project name.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-12-06 20:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function terminateIfAppExists(appRoot) {
|
2022-12-22 22:39:50 +00:00
|
|
|
if (fs.existsSync(appRoot)) {
|
|
|
|
console.error(
|
|
|
|
`Cannot create a project because it already exists by the name: ${appRoot}`
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-12-06 20:03:34 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 22:30:02 +00:00
|
|
|
function readDirNames(target) {
|
2022-12-22 22:39:50 +00:00
|
|
|
return fs
|
|
|
|
.readdirSync(target, { withFileTypes: true })
|
|
|
|
.filter((dir) => dir.isDirectory())
|
|
|
|
.map((dir) => dir.name)
|
|
|
|
.reduce((acc, name) => {
|
|
|
|
acc[name] = path.resolve(target, name);
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildChoices(examples) {
|
|
|
|
// handle a case if default example will be deleted without updating this place
|
|
|
|
if (!examples[DEFAULT_EXAMPLE]) {
|
|
|
|
return Object.keys(examples);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
DEFAULT_EXAMPLE,
|
|
|
|
...Object.keys(examples).filter((v) => v !== DEFAULT_EXAMPLE),
|
|
|
|
];
|
2022-12-21 22:30:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 22:39:50 +00:00
|
|
|
module.exports = { init };
|