我有自己的node_module https://www.npmjs.com/package/@aakashdeveloper/create-node-app
它是为启动新节点应用程序而创建的。 这个节点模块的问题是当我们安装它时,它可以像其他软件包一样下载到node_module文件夹中,并添加到package.json中。
我的要求是下载为起始文件夹。 谁能帮忙。
问题主要与package.json有关,有些地方我确实不确定“ bin”和“ file”。
{
"name": "@aakashdeveloper/create-node-app",
"version": "1.0.33",
"description": "The Seed will help you build node app with es6 very quick",
"main": "index.js",
"scripts": {
"start": "node start.js",
"dev": "nodemon start.js",
"test": "mocha --timeout 10000",
},
"repository": {
"type": "git",
"url": "git+https://github.com/Aakashdeveloper/create-node-app"
},
"keywords": [
"Node",
],
"author": "aakashdeveloper",
"license": "MIT",
"bugs": {
"url": "https://github.com/Aakashdeveloper/create-node-app/issues"
},
"homepage": "https://github.com/Aakashdeveloper/create-node-app#readme",
"dependencies": {
"@babel/core": "^7.4.3",
}
}
我需要像creata-react-app一样下载。
答案 0 :(得分:0)
我认为您误解了create-react-app
流程或类似的模块。您需要将模块安装为全局模块,并公开cli
,通过它可以在给定目录中创建项目文件。您可以使用yargs
处理命令和参数,然后在给定目录中创建必要的文件。以下是示例代码(未经测试),您可以在其中找到基本的想法。
#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
// the dir where the base files are stored
// in case you use the copyFiles method
const baseFilesDir = path.join(__dirname, "base");
// get the dir from the user.
const dir = getDirFromArg(process.argv[2]);
if(!dir){
console.log('Please specify a directory to create project');
}
// create node app in the given directory
createNodeAppInDir(dir);
/****** HELPERS *****/
function getDirFromArg(arg){
switch(arg){
case '.':
case '..':
return path.normalize(__dirname, arg);
default:
return path.normalize(arg);
}
}
async function createNodeAppInDir(dir){
const dirExists = await checkDir(dir);
if(!dirExists){
try{
await createDir(dir);
}
catch(e){
// display the error to user and break the operation
// may be there is a permission issue
console.error(e);
return false;
}
}
// now copy base files to the directory
// or create new files in there, your choice
// copying would be my preference though
try{
await copyBaseFiles(dir);
}
catch(e){
console.error(e);
return false;
}
}
// check if the directory exists
const checkDir = (dir) => {
return new Promise((resolve, reject) => {
fs.access(dir, (err) => {
if(err){
// directory doesn't exist
return resolve(false);
}
// direcotry exists
return resolve(true);
});
});
}
// create directory
const createDir = (dir) => {
return new Promise((resolve, reject) => {
fs.mkdir(dir, { recursive: true }, (err) => {
if(err){
// unable to create directory
return reject(err);
}
// direcotry successfully created
return resolve(true);
});
});
}
const copyBaseFiles = (dir) => {
// copy the contents of the src dir to the destination dir
copyDir(baseFilesDir, dir);
}
// credit:: https://gist.github.com/tkihira/3014700
var mkdir = function(dir) {
// making directory without exception if exists
try {
fs.mkdirSync(dir, 0755);
} catch(e) {
if(e.code != "EEXIST") {
throw e;
}
}
};
var rmdir = function(dir) {
if (path.existsSync(dir)) {
var list = fs.readdirSync(dir);
for(var i = 0; i < list.length; i++) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if(filename == "." || filename == "..") {
// pass these files
} else if(stat.isDirectory()) {
// rmdir recursively
rmdir(filename);
} else {
// rm fiilename
fs.unlinkSync(filename);
}
}
fs.rmdirSync(dir);
} else {
console.warn("warn: " + dir + " not exists");
}
};
var copyDir = function(src, dest) {
mkdir(dest);
var files = fs.readdirSync(src);
for(var i = 0; i < files.length; i++) {
var current = fs.lstatSync(path.join(src, files[i]));
if(current.isDirectory()) {
copyDir(path.join(src, files[i]), path.join(dest, files[i]));
} else if(current.isSymbolicLink()) {
var symlink = fs.readlinkSync(path.join(src, files[i]));
fs.symlinkSync(symlink, path.join(dest, files[i]));
} else {
copy(path.join(src, files[i]), path.join(dest, files[i]));
}
}
};
var copy = function(src, dest) {
var oldFile = fs.createReadStream(src);
var newFile = fs.createWriteStream(dest);
oldFile.pipe(newFile);
};