mirror of
https://github.com/waku-org/js-waku-examples.git
synced 2025-02-23 05:08:15 +00:00
make light-chat as a first option
This commit is contained in:
parent
53da2e44e0
commit
5e50b66bf5
@ -6,6 +6,8 @@ const execSync = require("child_process").execSync;
|
|||||||
const { Command } = require("commander");
|
const { Command } = require("commander");
|
||||||
const validateProjectName = require("validate-npm-package-name");
|
const validateProjectName = require("validate-npm-package-name");
|
||||||
|
|
||||||
|
const DEFAULT_EXAMPLE = "light-chat";
|
||||||
|
|
||||||
const supportedExamplesDir = path.resolve(__dirname, "./examples");
|
const supportedExamplesDir = path.resolve(__dirname, "./examples");
|
||||||
const supportedExamples = readDirNames(supportedExamplesDir);
|
const supportedExamples = readDirNames(supportedExamplesDir);
|
||||||
|
|
||||||
@ -17,8 +19,11 @@ const init = async (name, description, version) => {
|
|||||||
.name(name)
|
.name(name)
|
||||||
.description(description)
|
.description(description)
|
||||||
.version(version, "-v, --version", "output the version number")
|
.version(version, "-v, --version", "output the version number")
|
||||||
.arguments("<project-directory>", "Project directory to initialize Waku app")
|
.arguments(
|
||||||
.action(_appName => {
|
"<project-directory>",
|
||||||
|
"Project directory to initialize Waku app"
|
||||||
|
)
|
||||||
|
.action((_appName) => {
|
||||||
appName = _appName;
|
appName = _appName;
|
||||||
})
|
})
|
||||||
.option(
|
.option(
|
||||||
@ -35,7 +40,7 @@ const init = async (name, description, version) => {
|
|||||||
const templatePrompt = new enquirer.Select({
|
const templatePrompt = new enquirer.Select({
|
||||||
name: "template",
|
name: "template",
|
||||||
message: "Select template",
|
message: "Select template",
|
||||||
choices: Object.keys(supportedExamples),
|
choices: buildChoices(supportedExamples),
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -47,13 +52,18 @@ const init = async (name, description, version) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!supportedExamples[template]) {
|
if (!supportedExamples[template]) {
|
||||||
const supportedExamplesMessage = Object.keys(supportedExamples).reduce((acc, v) => {
|
const supportedExamplesMessage = Object.keys(supportedExamples).reduce(
|
||||||
|
(acc, v) => {
|
||||||
acc += `\t${v}\n`;
|
acc += `\t${v}\n`;
|
||||||
return acc;
|
return acc;
|
||||||
}, "");
|
},
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
console.error(`Unknown template: ${template}`);
|
console.error(`Unknown template: ${template}`);
|
||||||
console.error(`We support only following templates:\n${supportedExamplesMessage}`)
|
console.error(
|
||||||
|
`We support only following templates:\n${supportedExamplesMessage}`
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,9 +131,13 @@ function terminateIfProjectNameInvalid(name) {
|
|||||||
const validationResult = validateProjectName(name);
|
const validationResult = validateProjectName(name);
|
||||||
|
|
||||||
if (!validationResult.validForNewPackages) {
|
if (!validationResult.validForNewPackages) {
|
||||||
console.error(`Cannot create a project named ${name} because of npm naming restrictions:\n`);
|
console.error(
|
||||||
[...(validationResult.errors || []), ...(validationResult.warnings || [])]
|
`Cannot create a project named ${name} because of npm naming restrictions:\n`
|
||||||
.forEach(error => console.error(` * ${error}`));
|
);
|
||||||
|
[
|
||||||
|
...(validationResult.errors || []),
|
||||||
|
...(validationResult.warnings || []),
|
||||||
|
].forEach((error) => console.error(` * ${error}`));
|
||||||
console.error("\nPlease choose a different project name.");
|
console.error("\nPlease choose a different project name.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
@ -131,19 +145,34 @@ function terminateIfProjectNameInvalid(name) {
|
|||||||
|
|
||||||
function terminateIfAppExists(appRoot) {
|
function terminateIfAppExists(appRoot) {
|
||||||
if (fs.existsSync(appRoot)) {
|
if (fs.existsSync(appRoot)) {
|
||||||
console.error(`Cannot create a project because it already exists by the name: ${appRoot}`);
|
console.error(
|
||||||
|
`Cannot create a project because it already exists by the name: ${appRoot}`
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function readDirNames(target) {
|
function readDirNames(target) {
|
||||||
return fs.readdirSync(target, { withFileTypes: true })
|
return fs
|
||||||
.filter(dir => dir.isDirectory())
|
.readdirSync(target, { withFileTypes: true })
|
||||||
.map(dir => dir.name)
|
.filter((dir) => dir.isDirectory())
|
||||||
|
.map((dir) => dir.name)
|
||||||
.reduce((acc, name) => {
|
.reduce((acc, name) => {
|
||||||
acc[name] = path.resolve(target, name);
|
acc[name] = path.resolve(target, name);
|
||||||
return acc;
|
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),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = { init };
|
module.exports = { init };
|
Loading…
x
Reference in New Issue
Block a user