-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 26d853a
Showing
7 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
lib | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020, Aymeric Assier and Julien Martins Da Costa. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@wide/scaffolder", | ||
"version": "1.0.0", | ||
"description": "Automatised project generator.", | ||
"author": "Aymeric Assier (https://github.com/myeti)", | ||
"contributors": [ | ||
"Aymeric Assier (https://github.com/myeti)", | ||
"Julien Martins Da Costa (https://github.com/jdacosta)" | ||
], | ||
"main": "lib", | ||
"engines": { | ||
"node": ">=10.13.0" | ||
}, | ||
"scripts": { | ||
"build": "babel src/ -d lib/ --presets @babel/preset-env", | ||
"prepare": "rimraf lib/ && npm run build" | ||
}, | ||
"dependencies": { | ||
"chalk": "^4.1.1", | ||
"fs-extra": "^10.0.0", | ||
"yargs": "^17.0.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.14.3", | ||
"@babel/preset-env": "^7.14.4", | ||
"rimraf": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Scaffolder | ||
|
||
Automatised project generator. | ||
|
||
|
||
## Install | ||
|
||
``` | ||
npm install @wide/scaffolder --save | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
Create a CLI script: | ||
|
||
```js | ||
#!/usr/bin/env node | ||
import scaffold from '@wide/scaffolder' | ||
import pkg from './package.json' | ||
|
||
scaffold(__dirname, { | ||
template: 'template', | ||
pkg | ||
}) | ||
``` | ||
|
||
Then call your script: | ||
``` | ||
node ./script.js my-app | ||
``` | ||
|
||
When calling your script using CLI or NPX, it will: | ||
- create a folder named `my-app` | ||
- copy the `template` folder inside | ||
- replace `project-name` with `my-app` in `package.json` | ||
- replace `project-name` with `my-app` in `readme.md` | ||
- install all dependencies | ||
- start your project | ||
|
||
|
||
## Authors | ||
|
||
- **Aymeric Assier** - [github.com/myeti](https://github.com/myeti) | ||
- **Julien Martins Da Costa** - [github.com/jdacosta](https://github.com/jdacosta) | ||
|
||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [licence](licence) file for details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { argv } from 'yargs' | ||
import chalk from 'chalk' | ||
import path from 'path' | ||
import DEFAULT_TASKS from './tasks' | ||
|
||
const DEFAULT_CONFIG = { | ||
template: 'template', | ||
plop: 'plop', | ||
pkg: {} | ||
} | ||
|
||
export default function(root, cfg) { | ||
|
||
// resolve config | ||
const config = Object.assign({}, DEFAULT_CONFIG, cfg) | ||
console.log(chalk`{blue.bold ${config.pkg.name}} v${config.pkg.version}`) | ||
|
||
// resolve base date | ||
const data = { | ||
cwd: process.cwd(), | ||
name: argv._[0] | ||
} | ||
|
||
// check name | ||
if(!data.name) { | ||
console.log(chalk`{red.bold error, you must specify a project name}`) | ||
process.exit() | ||
} | ||
|
||
// resolve folders and files | ||
data.template = path.resolve(root, config.template), | ||
data.dest = path.resolve(data.cwd, data.name) | ||
data.packageJson = path.resolve(data.dest, 'package.json') | ||
data.readme = path.resolve(data.dest, 'readme.md') | ||
|
||
// resolve custom data | ||
if(cfg.resolve) { | ||
const resolved = cfg.resolved(data, argv) | ||
Object.assign(data, resolved) | ||
} | ||
|
||
// generate factory out of tasks | ||
const tasks = {} | ||
const taskDefs = Object.assign({}, DEFAULT_TASKS, cfg.tasks) | ||
for(let name in taskDefs) { | ||
tasks[name] = function() { | ||
const label = taskDefs[name].label(data).replace(/\*(.+)\*/g, (a, b) => chalk.cyan.bold(b)) | ||
console.log(chalk`{gray #}`, label) | ||
taskDefs[name].run(data) | ||
console.log(chalk` {green.bold ✓} ok`) | ||
} | ||
} | ||
|
||
// run tasks | ||
cfg.run = cfg.run || function(tasks, data, argv) { | ||
tasks.createFolder() | ||
tasks.copyTemplate() | ||
tasks.writePackageJson() | ||
tasks.writeReadme() | ||
tasks.npmInstall() | ||
tasks.npmStart() | ||
} | ||
cfg.run(tasks, data, argv) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { mkdir, copy, rewrite, exec } from './utils' | ||
|
||
export default { | ||
createFolder: { | ||
label: data => `create folder *${data.name}*`, | ||
run: data => mkdir(data.dest) | ||
}, | ||
copyTemplate: { | ||
label: data => 'copy working files', | ||
run: data => copy(data.template, data.dest) | ||
}, | ||
writePackageJson: { | ||
label: data => 'update *package.json*', | ||
run: data => rewrite(data.packageJson, { 'project-name': data.name }) | ||
}, | ||
writeReadme: { | ||
label: data => 'update *readme*', | ||
run: data => rewrite(data.readme, { 'project-name': data.name }) | ||
}, | ||
npmInstall: { | ||
label: data =>'install dependencies', | ||
run: data => exec(`cd ${data.name} && npm install`) | ||
}, | ||
npmStart: { | ||
label: data => 'your project is now starting:', | ||
run: data => exec(`cd ${data.name} && npm start`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { execSync } from 'child_process' | ||
import fse from 'fs-extra' | ||
import fs from 'fs' | ||
|
||
|
||
/** | ||
* Create a folder | ||
* @param {String} name | ||
*/ | ||
export function mkdir(name) { | ||
return fs.mkdirSync(name) | ||
} | ||
|
||
|
||
/** | ||
* Copy a folder | ||
* @param {String} from | ||
* @param {String} to | ||
*/ | ||
export function copy(from, to) { | ||
return fse.copySync(from, to) | ||
} | ||
|
||
|
||
/** | ||
* Replace content in file | ||
* @param {String} file | ||
* @param {Object<string, String>} interpolate | ||
*/ | ||
export function rewrite(file, interpolate) { | ||
let content = fs.readFileSync(file).toString() | ||
for(let placeholder in interpolate) { | ||
content = content.replace(placeholder, interpolate[placeholder]) | ||
} | ||
return fs.writeFileSync(file, content) | ||
} | ||
|
||
|
||
/** | ||
* Run CLI command | ||
* @param {String} command | ||
*/ | ||
export function exec(command) { | ||
return execSync(command, { stdio: 'inherit' }) | ||
} |