Merge pull request #170 from waku-org/weboko/create-waku-imp

Follow-up improvements to @waku/app
This commit is contained in:
Sasha 2023-01-04 22:43:31 +01:00 committed by GitHub
commit 266916be74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
173 changed files with 355 additions and 184 deletions

View File

@ -33,12 +33,12 @@ jobs:
- name: install
run: npm install
working-directory: ${{ matrix.example }}
working-directory: "examples/${{ matrix.example }}"
- name: build
run: npm run build
working-directory: ${{ matrix.example }}
working-directory: "examples/${{ matrix.example }}"
- name: test
run: npm run test --if-present
working-directory: ${{ matrix.example }}
working-directory: "examples/${{ matrix.example }}"

6
ci/Jenkinsfile vendored
View File

@ -48,6 +48,7 @@ pipeline {
stage('store-js') { steps { script { copyExample() } } }
stage('light-js') { steps { script { copyExample() } } }
stage('rln-js') { steps { script { copyExample() } } }
stage('light-chat') { steps { script { copyExample() } } }
}
}
@ -68,7 +69,7 @@ pipeline {
def buildExample(example=STAGE_NAME) {
def dest = "${WORKSPACE}/build/docs/${example}"
dir("${example}") {
dir("examples/${example}") {
sh 'npm install --silent'
sh 'npm run build'
sh "mkdir -p ${dest}"
@ -78,6 +79,5 @@ def buildExample(example=STAGE_NAME) {
def copyExample(example=STAGE_NAME) {
sh "mkdir -p build/docs/${example}"
sh "cp ${example}/index.html build/docs/${example}/"
sh "[ -f ${example}/style.css ] && cp ${example}/style.css build/docs/${example}/ || exit 0"
sh "cp examples/${example}/*.(js|css|html) build/docs/${example}/"
}

29
create-waku-app/build.js Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
const examplesSource = path.resolve(__dirname, "../examples");
const examplesDestination = path.resolve(__dirname, "./examples");
function run() {
fs.ensureDirSync(examplesDestination);
try {
console.log("Started copying supported Waku examples.");
fs.copySync(examplesSource, examplesDestination, { filter: nodeModulesFiler });
console.log("Finished copying examples.");
} catch (error) {
console.error("Failed to copy examples due to " + error.message);
throw Error(error.message);
}
}
function nodeModulesFiler(src) {
if (src.includes("node_modules")) {
return false;
}
return true;
}
run();

View File

@ -0,0 +1,178 @@
const path = require("path");
const fs = require("fs-extra");
const enquirer = require("enquirer");
const execSync = require("child_process").execSync;
const { Command } = require("commander");
const validateProjectName = require("validate-npm-package-name");
const DEFAULT_EXAMPLE = "light-chat";
const supportedExamplesDir = path.resolve(__dirname, "./examples");
const supportedExamples = readDirNames(supportedExamplesDir);
const init = async (name, description, version) => {
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),
});
try {
template = await templatePrompt.run();
} catch (e) {
console.error(`Failed at selecting a template: ${e.message}`);
process.exit(1);
}
}
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.ensureDirSync(appName);
fs.copySync(templateDir, appRoot);
runNpmInApp(appRoot);
runGitInit(appRoot);
}
function runNpmInApp(root) {
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);
}
}
function runGitInit(root) {
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);
}
}
function isInGitRepository() {
try {
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
return true;
} catch (e) {
return false;
}
}
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);
}
}
function readDirNames(target) {
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),
];
}
module.exports = { init };

View File

@ -16,4 +16,4 @@ if (!semver.satisfies(currentNodeVersion, supportedNodeVersion)) {
const { init } = require("./createApp");
init(packageJson.name, packageJson.description, packageJson.version, packageJson.wakuExamples);
init(packageJson.name, packageJson.description, packageJson.version);

View File

@ -35,19 +35,9 @@
"dapps"
],
"license": "MIT OR Apache-2.0",
"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",
"enquirer": "^2.3.6",
"fs-extra": "^11.1.0",
"semver": "^7.3.8",
"validate-npm-package-name": "^5.0.0"

View File

@ -16606,6 +16606,7 @@
"version": "8.0.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz",
"integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==",
"dev": true,
"dependencies": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
@ -16642,6 +16643,7 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
"dev": true,
"dependencies": {
"balanced-match": "^1.0.0"
}
@ -16650,6 +16652,7 @@
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.2.tgz",
"integrity": "sha512-bNH9mmM9qsJ2X4r2Nat1B//1dJVcn3+iBLa3IgqJ7EbGaDNepL9QSHOxN4ng33s52VMMhhIfgCYDk3C4ZmlDAg==",
"dev": true,
"dependencies": {
"brace-expansion": "^2.0.1"
},
@ -22429,6 +22432,7 @@
"version": "0.5.6",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
"dev": true,
"dependencies": {
"minimist": "^1.2.6"
},
@ -47798,6 +47802,7 @@
"version": "8.0.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz",
"integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==",
"dev": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
@ -47810,6 +47815,7 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
"dev": true,
"requires": {
"balanced-match": "^1.0.0"
}
@ -47818,6 +47824,7 @@
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.2.tgz",
"integrity": "sha512-bNH9mmM9qsJ2X4r2Nat1B//1dJVcn3+iBLa3IgqJ7EbGaDNepL9QSHOxN4ng33s52VMMhhIfgCYDk3C4ZmlDAg==",
"dev": true,
"requires": {
"brace-expansion": "^2.0.1"
}
@ -52005,6 +52012,7 @@
"version": "0.5.6",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
"dev": true,
"requires": {
"minimist": "^1.2.6"
}

View File

@ -27,7 +27,7 @@
"test": "run-s build test:*",
"test:lint": "eslint src --ext .ts --ext .tsx",
"test:prettier": "prettier \"src/**/*.{ts,tsx}\" \"./*.json\" --list-different",
"test:spelling": "cspell \"{README.md,src/**/*.{ts,tsx},public/**/*.html}\" -c ../.cspell.json",
"test:spelling": "cspell \"{README.md,src/**/*.{ts,tsx},public/**/*.html}\" -c ./.cspell.json",
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" \"./*.json\" --write",
"fix:lint": "eslint src --ext .ts --ext .tsx --fix"
},

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

Some files were not shown because too many files have changed in this diff Show More