Nodejs-プロジェクト構造初期化事例
3205 ワード
-- `npm init -y`
-- index.js
-- init.js
-- config.json
index.html 404.html
-- path、fs
{
"rootName" : "mydemo",
"data" : [
{
"name" : "img",
"type" : "dir"
},
{
"name" : "js",
"type" : "dir"
},
{
"name" : "css",
"type" : "dir"
},
{
"name" : "index.html",
"type" : "file"
},
{
"name" : "404.html",
"type" : "file"
}
]
}
//
const cfg = require('./config.json');
const path = require('path');
const fs = require('fs')
//
let rootName = cfg.rootName;
//
let init = () => {
fs.mkdir(path.join('./', rootName), (err) => {
if (err) {
console.log(" ");
return;
}
cfg.data.forEach(obj => {
if (obj.type == 'dir') {
fs.mkdir(path.join('./', rootName, obj.name), (err) => {
if (err) {
consle.log(" ");
return;
}
console.log(`${obj.name} `)
})
}else if (obj.type == 'file') {
fs.readFile(path.join(__dirname,obj.name),(err, data) => {
if (err) {
console.log(" ");
return;
}
fs.writeFile(path.join('./',rootName, obj.name),data, (err) => {
if (err) {
console.log(" ");
return;
}
console.log(`${obj.name} `);
})
})
}
})
})
}
// init();
exports.init = init;
//
const obj = require('./init.js');
let param = process.argv[2];
switch(param){
case '-i':
obj.init();
break;
case '-h':
console.log(' ');
break;
case '-v':
console.log('1.0.0');
break;
}
1. node .
2. node . , ,
node , 6.9.1
C:\dev
vm\v6.9.1
ode_modules
cmd , init.cmd
@IF EXIST "%~dp0
ode.exe" (
"%~dp0
ode.exe" "%~dp0
ode_modules\wjj\index.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0
ode_modules\wjj\index.js" %*
)
init
init -v init -h
, ./