create build scrupt, make adjustments to use examples from package json

This commit is contained in:
weboko 2022-12-06 21:03:34 +01:00 committed by weboko
parent e7f68732cb
commit 874c2477a8
No known key found for this signature in database
5 changed files with 181 additions and 43 deletions

View File

@ -0,0 +1,42 @@
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
const packageJson = require("./package.json");
const examplesFolder = path.resolve("./examples");
async function run() {
fs.ensureDirSync(examplesFolder);
const supportedExamples = Object.entries(packageJson.wakuExamples);
console.log("Started copying supported Waku examples.");
const copyPromises = supportedExamples.map(([name, relativePath]) => {
const resolvedPath = path.resolve(__dirname, relativePath);
const destinationPath = path.resolve(examplesFolder, name);
return fs.copy(resolvedPath, destinationPath, { filter: nodeModulesFiler }).catch((error) => {
console.error(`Failed to copy example ${name} to ${destinationPath} with ${error.message}`);
throw Error(error.message);
});
});
await Promise.all(copyPromises)
.then(() => {
console.log("Finished copying examples.");
})
.catch((error) => {
console.error("Failed to copy examples due to " + error.message);
});
}
function nodeModulesFiler(src) {
if (src.includes("node_modules")) {
return false;
}
return true;
}
run();

View File

@ -0,0 +1,76 @@
const fs = require("fs-extra");
const path = require("path");
const { Command } = require('commander');
const validateProjectName = require('validate-npm-package-name');
const supportedExamplesDir = path.resolve("./examples");
const init = (name, description, version, supportedExamples) => {
let appName;
const program = 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"
)
.allowUnknownOption()
.parse();
const options = program.opts();
const template = options.template || "web-chat";
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);
};
function createApp(name, template) {
const appRoot = path.resolve(name);
const appName = path.basename(appRoot);
const templateDir = path.resolve(supportedExamplesDir, template);
terminateIfAppExists(appName);
terminateIfProjectNameInvalid(appName);
console.log(`Initializing ${appName} from ${template} template.`);
fs.copySync(templateDir, appRoot);
// initNpmOrYarn(appRoot);
}
function terminateIfProjectNameInvalid(name) {
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);
}
}
function terminateIfAppExists(appRoot) {
if (fs.existsSync(appRoot)) {
console.error(`Cannot create a project because it already exists by the name: ${appRoot}`);
process.exit(1);
}
}
module.exports = { init };

View File

@ -1,28 +0,0 @@
const { Command } = require('commander');
const packageJson = require('./package.json');
let appName;
const createApp = () => {
const program = new Command()
.name(packageJson.name)
.description(packageJson.description)
.version(packageJson.version)
.arguments("<project-directory>", "Project directory to initialize Waku app")
.action(name => {
appName = name;
})
.option(
"-t, --template <path-to-template>",
"specify a template for the created project"
)
.allowUnknownOption()
.parse();
const options = program.opts();
console.log(`Initializing ${appName} from template ${options.template || "default"}`);
};
module.exports = { createApp };

View File

@ -1,4 +1,19 @@
#!/usr/bin/env node
const { createApp } = require("./createWakuApp");
const packageJson = require("./package.json");
const semver = require("semver");
createApp();
const currentNodeVersion = process.versions.node;
const supportedNodeVersion = packageJson.engines.node;
if (!semver.satisfies(currentNodeVersion, supportedNodeVersion)) {
console.error(
`You are running Node ${currentNodeVersion}.\n` +
`@waku/create-app works only with ${packageJson.engines.node}.\n` +
`Please update your version of Node.`
);
process.exit(1);
}
const { init } = require("./createApp");
init(packageJson.name, packageJson.description, packageJson.version, packageJson.wakuExamples);

View File

@ -1,10 +1,30 @@
{
"name": "create-waku-app",
"version": "0.1.0",
"name": "create-dementor-app",
"version": "0.1.13",
"description": "Helper package to bootstrap Waku app ",
"repository": {
"type": "git",
"url": "https://github.com/waku-org/js-waku-examples.git",
"directory": "packages/create-app"
},
"engines": {
"node": ">=16"
},
"bugs": {
"url": "https://github.com/waku-org/js-waku-examples/issues"
},
"files": [
"index.js",
"createApp.js",
"examples"
],
"main": "index.js",
"bin": {
"create-waku-app": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "node ./build.js",
"prepublishOnly": "npm run build"
},
"keywords": [
"waku",
@ -14,9 +34,22 @@
"ethereum",
"dapps"
],
"license": "MIT OR Apache-2.0",
"license": "MIT",
"wakuExamples": {
"eth-pm": "../../eth-pm",
"light-js": "../../light-js",
"relay-angular-chat": "../../relay-angular-chat",
"relay-js": "../../relay-js",
"relay-reactjs-chat": "../../relay-reactjs-chat",
"rln-js": "../../rln-js",
"store-js": "../../store-js",
"store-reactjs-chat": "../../store-reactjs-chat",
"web-chat": "../../web-chat"
},
"dependencies": {
"commander": "^9.4.1"
"commander": "^9.4.1",
"fs-extra": "^11.1.0",
"semver": "^7.3.8",
"validate-npm-package-name": "^5.0.0"
}
}
}