Comparing version 0.1.6 to 1.0.0
@@ -1,24 +0,60 @@ | ||
const fs = require("fs") | ||
const ejs = require("ejs") | ||
const path = require("path") | ||
// @ts-check | ||
module.exports = (filepath, newpath, values) => { | ||
const filename = path.basename(filepath) | ||
/** | ||
* Evaluate ejs template file and create a new file | ||
* with the evaluated template content in it | ||
* @module eval | ||
*/ | ||
if(fs.existsSync(filepath) && fs.lstatSync(filepath).isDirectory()){ | ||
return false | ||
} | ||
const fs = require("fs"); | ||
const ejs = require("ejs"); | ||
const path = require("path"); | ||
if(filename[0] === "_"){ | ||
const content = fs.readFileSync(filepath) | ||
const eval_content = ejs.render(content.toString(), values) | ||
/** | ||
* Process only ejs files and copies to newpath | ||
* returns true if not an ejs file or file doesn't exist. | ||
* | ||
* @param {string} filepath - source file path | ||
* @param {string} newpath - path to the target file | ||
* @param {object} variables - variables to be resolved in ejs template | ||
* | ||
* @return {Promise<void>} | ||
*/ | ||
module.exports = (filepath, newpath, variables) => { | ||
const dirname = path.dirname(newpath); | ||
newpath = path.join(path.dirname(newpath), filename.substr(1)) | ||
fs.mkdirSync(dirname, { recursive: true }); | ||
fs.writeFileSync(newpath, eval_content) | ||
return new Promise((resolve, reject) => { | ||
const filename = path.basename(filepath); | ||
return false | ||
} | ||
if (fs.existsSync(filepath) && fs.lstatSync(filepath).isDirectory()) { | ||
resolve(); | ||
} | ||
return true | ||
} | ||
if (filename[0] === "_") { | ||
const content = fs.readFileSync(filepath, { | ||
encoding: "utf8", | ||
}); | ||
try { | ||
const eval_content = ejs.render(content.toString(), variables); | ||
newpath = path.join(path.dirname(newpath), filename.substr(1)); | ||
fs.writeFileSync(newpath, eval_content); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
return resolve(); | ||
} else { | ||
try { | ||
fs.copyFileSync(filepath, newpath); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
resolve(); | ||
}); | ||
}; |
@@ -1,35 +0,72 @@ | ||
const fs = require("fs") | ||
const path = require("path") | ||
const glob = require("glob") | ||
const eval = require("./eval.js") | ||
// @ts-check | ||
module.exports = (templateDir, outDir, values, cb) => { | ||
fs.mkdir(outDir, (err) => { | ||
if (err) return cb(err) | ||
/** | ||
* Create a project from a template | ||
* @module generator | ||
*/ | ||
outDir = path.resolve(outDir) | ||
/** | ||
* @typedef {object} Configuration | ||
* @property {boolean} overwrite - will overwrite the target directory if true, by default false | ||
* @property {object} variables - variables to be resolved in ejs template | ||
*/ | ||
glob("**/*", { cwd: templateDir }, (err, res) => { | ||
if (err) return cb(err) | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const glob = require("glob"); | ||
const evaluate = require("./eval.js"); | ||
for(file of res){ | ||
const filepath = path.join(templateDir, file) | ||
/** | ||
* Generates a project from the template | ||
* | ||
* Files with their name starting with _ (underscore) | ||
* can support ejs template and will get evaluated as an ejs file | ||
* | ||
* @param {string} templateDir - Path to the template directory | ||
* @param {string} outDir - Output directory | ||
* @param {Configuration} config - Map from variable to its value; | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
module.exports = ( | ||
templateDir, | ||
outDir, | ||
config = { | ||
overwrite: false, | ||
variables: {}, | ||
} | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
fs.access(templateDir, (err) => { | ||
if (err) return reject(err); | ||
}); | ||
newpath = path.join(outDir, file) | ||
dirname = path.dirname(newpath) | ||
const options = { | ||
recursive: config.overwrite, | ||
}; | ||
fs.mkdirSync(dirname, { recursive: true }) | ||
fs.mkdir(outDir, options, (err) => { | ||
if (err) return reject(err); | ||
if(eval(filepath, newpath, values)){ | ||
outDir = path.resolve(outDir); | ||
glob("**/*", { cwd: templateDir }, async (err, res) => { | ||
if (err) return reject(err); | ||
for (const file of res) { | ||
const filepath = path.join(templateDir, file); | ||
const newpath = path.join(outDir, file); | ||
try { | ||
fs.copyFileSync(filepath, newpath) | ||
await evaluate(filepath, newpath, config.variables); | ||
} catch (err) { | ||
cb(err) | ||
return reject(err); | ||
} | ||
} | ||
} | ||
cb() | ||
}) | ||
}) | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "scaffe", | ||
"version": "0.1.6", | ||
"version": "1.0.0", | ||
"description": "A Scaffolding Utility", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
@@ -23,3 +26,7 @@ "type": "git", | ||
"glob": "^7.1.6" | ||
}, | ||
"devDependencies": { | ||
"jest": "^26.6.3", | ||
"mock-fs": "^4.13.0" | ||
} | ||
} |
@@ -1,7 +0,14 @@ | ||
# scaffe.js | ||
<img src="https://raw.githubusercontent.com/sinix-dev/scaffe.js/feat/release-changes/.github/assets/scaffe-logo.png" width="400"> | ||
<hr> | ||
[](https://github.com/sinix-dev/scaffe.js/actions/workflows/test.js.yml) | ||
[](https://www.npmjs.com/package/scaffe) | ||
[](https://github.com/sinix-dev/scaffe.js/blob/master/LICENSE) | ||
Simple scaffolding utility, inspired by [Sao.js](https://github.com/saojs/sao) | ||
### Installation | ||
```bash | ||
$ npm install scaffe --save | ||
$ npm install scaffe # yarn add scaffe | ||
``` | ||
@@ -14,28 +21,39 @@ | ||
... | ||
async function build(){ | ||
... | ||
try { | ||
await scaffe.generate(templateDir, outDir, { overwrite: true, variables: { name: "app" } }); | ||
} catch(err) { | ||
console.log(err) | ||
} | ||
scaffe.generate(template_dir, outDir, { name: appName }, (err) => { | ||
if(err){ | ||
console.error(err) | ||
} else { | ||
console.log("Successfully generated") | ||
} | ||
}) | ||
// OR | ||
scaffe.generate(templateDir, outDir, { overwrite: true, variables: { name: "app" }).catch((err) => { | ||
console.log(err); | ||
}) | ||
scaffe.generate(templateDir, outDir, variables, (err) => { | ||
console.log(err); | ||
}) | ||
} | ||
``` | ||
### More Info | ||
The only available function in Scaffe is `generate` which takes arguments as | ||
following in order. | ||
`templateDir`: It's the path to the template directory. <br> | ||
`outDir`: The output directory <br> | ||
`values`: An Object mapped from variables to it's values <br> | ||
`cb`: A callback function that have `err` as the only argument <br> | ||
- `templateDir`: It's the path to the template directory. <br> | ||
- `outDir`: The output directory <br> | ||
- `config`: An Object with two props `{boolean} overwrite (by default false)`, `{object} variables` <br> | ||
Template directory can have two types files <br> | ||
`starts with _`: this file will be evaluated as an ejs file <br> | ||
`doesn't starts with _`: this type of files will be copied as it is to the output directory. | ||
**Template directory can have two types of files** <br> | ||
- `starts with _`: this file will be evaluated as an ejs file <br> | ||
- `doesn't starts with _`: this type of files will be copied as it is to the output directory. | ||
So we can uses variables in our template files in [ejs](https://ejs.co/) format. | ||
So we can use variables in our template files in [ejs](https://ejs.co/) format. | ||
A use case, | ||
**A use case,** | ||
```javascript | ||
@@ -49,3 +67,3 @@ // _package.json | ||
`values` i.e. 3rd argument; will contain all variable values that | ||
needs to be passed on to pe processed by [ejs](https://ejs.co/). | ||
`variables` in 3rd argument (i.e. config) will contain all variable values that | ||
need to be passed on to be processed by [ejs](https://ejs.co/). |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20636
12
307
0
68
2
4