Skip to content

Commit cb05ff0

Browse files
author
Élio Cró
committed
Added basic create procedure
1 parent 96ecde4 commit cb05ff0

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

app/create.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const packageJson = require('../package.json');
4+
5+
const chalk = require('chalk');
6+
const commander = require('commander');
7+
const fs = require('fs-extra');
8+
const path = require('path');
9+
10+
11+
let projectName;
12+
13+
const program = new commander.Command(packageJson.name)
14+
.version(packageJson.version)
15+
.arguments('<project-directory>')
16+
.usage(`${chalk.green('<project-directory>')} [options]`)
17+
.action(name => {
18+
projectName = name;
19+
})
20+
.allowUnknownOption()
21+
.on('--help', () => {
22+
console.log(` Only ${chalk.green('<project-directory>')} is required.`);
23+
console.log();
24+
})
25+
.parse(process.argv);
26+
27+
if (typeof projectName === 'undefined') {
28+
console.error('Please specify the project directory:');
29+
console.log(` ${chalk.cyan(program.name())} ${chalk.green('<project-directory>')}`);
30+
console.log();
31+
console.log('For example:');
32+
console.log(` ${chalk.cyan(program.name())} ${chalk.green('my-block')}`);
33+
console.log();
34+
console.log(`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`);
35+
process.exit(1);
36+
}
37+
38+
39+
40+
// 1. Create the directory
41+
42+
const root = path.resolve(projectName);
43+
const appName = path.basename(root);
44+
const base = path.resolve('./examples/1-simple-block');
45+
46+
// TODO: Check and validate the appName
47+
48+
49+
// 1.1 Create the directory
50+
fs.ensureDir(root);
51+
52+
console.log(`Creating a new Cloud Block in ${chalk.green(root)}.`);
53+
54+
55+
// 1.2 Copy files from the example
56+
console.log(`Copying files...`);
57+
58+
fs.copySync(base, root);
59+
60+
61+
// 1.3 Rename the project files
62+
console.log(`Renaming the project to ${appName}`);
63+
64+
65+
console.log(chalk.green('Done!'));

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
console.log('Create Cloud Block - Not implemented yet');
4+
const chalk = require('chalk');
5+
const currentNodeVersion = process.versions.node;
6+
const semver = currentNodeVersion.split('.');
7+
const major = semver[0];
8+
9+
if(major < 8) {
10+
console.error(
11+
chalk.red(
12+
'You are running Node ' + currentNodeVersion + '.\n' +
13+
'Create Cloud Block requires Node 8 or higher. \n' +
14+
'Please update your version of Node.'
15+
)
16+
);
17+
process.exit(1);
18+
}
19+
20+
process.on('unhandledRejection', err => {
21+
throw err;
22+
});
23+
24+
require('./app/create');

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"create-cloud-block": "./index.js"
1111
},
1212
"dependencies": {
13-
"eslint": "^5.6.1"
13+
"chalk": "^2.4.1",
14+
"commander": "^2.19.0",
15+
"envinfo": "^5.10.0",
16+
"eslint": "^5.6.1",
17+
"fs-extra": "^7.0.0"
1418
},
1519
"keywords": [
1620
"gutenberg",

0 commit comments

Comments
 (0)