mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-18 01:29:09 +00:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20389d7b33 | ||
|
|
d7a4362725 | ||
|
|
0a2d7c08f3 | ||
|
|
a92c471243 | ||
|
|
07b1c4b1f0 | ||
|
|
ec91fbf639 | ||
|
|
85aa9f953b | ||
|
|
d6195c6c1d | ||
|
|
b14e3d2d76 | ||
|
|
fa2ddca57e | ||
|
|
686b0bf227 | ||
|
|
0be29dd89e | ||
|
|
4f29a612a3 | ||
|
|
14b60ffc79 | ||
|
|
1275743764 | ||
|
|
b09299b432 | ||
|
|
45e8de3034 | ||
|
|
a3a77e2ab5 | ||
|
|
f36015962d | ||
|
|
e391f8f68f | ||
|
|
4ca7c6acb3 | ||
|
|
0fd996413b | ||
|
|
e64c0e4a45 | ||
|
|
3e42ac5a95 | ||
|
|
eecec82aaa | ||
|
|
6ec19fc4cd | ||
|
|
a5776d6108 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -57,4 +57,5 @@ typings/
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
examples/test-output
|
||||
examples/test-output/**/*
|
||||
!examples/test-output/.gitkeep
|
||||
|
||||
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@@ -4,18 +4,26 @@
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Debug Scaffold",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"protocol": "inspector",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"program": "${workspaceFolder}/test.ts",
|
||||
"outFiles": [
|
||||
"${workspaceRoot}/dist/test.js"
|
||||
],
|
||||
"env": {
|
||||
"NODE_ENV": "develop"
|
||||
},
|
||||
"sourceMaps": true,
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach by Process ID",
|
||||
"processId": "${command:PickProcess}"
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Debug Scaffold",
|
||||
"protocol": "inspector",
|
||||
"program": "${workspaceFolder}/dist/scaffold.js"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"typescript.tsdk": "./node_modules/typescript/lib"
|
||||
}
|
||||
7
.vscode/tasks.json
vendored
Normal file
7
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"command": "webpack",
|
||||
"isShellCommand": true,
|
||||
"args": [],
|
||||
"showOutput": "always"
|
||||
}
|
||||
152
README.md
152
README.md
@@ -1,12 +1,148 @@
|
||||
# scaffolder
|
||||
Scaffolder allows you to create your structured files based on templates.
|
||||
# simple-scaffold
|
||||
Simple Scaffold allows you to create your structured files based on templates.
|
||||
|
||||
### How to use
|
||||
## Install
|
||||
You can either use it as a command line tool or import into your own code and run from there.
|
||||
|
||||
#### Install
|
||||
You Scaffolder by using `npm`. Global flag is useful if you want easy cli access to it.
|
||||
|
||||
```
|
||||
npm install -g scaffolder
|
||||
```bash
|
||||
# npm
|
||||
npm install [-g] simple-scaffold
|
||||
# yarn
|
||||
yarn [global] add simple-scaffold
|
||||
```
|
||||
|
||||
## Use as a command line tool
|
||||
The first non-token argument (that has no `--` prefix) will be used as the scaffold name.
|
||||
The rest is ignored, of course except for the available arguments below.
|
||||
|
||||
```bash
|
||||
simple-scaffold MyComponent --template scaffolds/component/**/* \
|
||||
--output src/components \
|
||||
--locals myProp="propname",myVal=123
|
||||
```
|
||||
|
||||
You can add this as a script in your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"scripts": {
|
||||
"scaffold": "node node_modules/simple-scaffold/dist/cmd.js --template scaffolds/component/**/* --output src/components --locals myProp=\"propname\",myVal=123"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Scaffolding
|
||||
Scaffolding will replace {{vars}} in both the file name and its contents and put the transformed files
|
||||
in `<output>/<{{Name}}`.
|
||||
|
||||
Your context will be pre-populated with the following:
|
||||
- `{{Name}}`: CapitalizedName of the component
|
||||
- `{{name}}`: camelCasedName of the component
|
||||
|
||||
Any `locals` you add in the config will populate with their names wrapped in `{{` and `}}`.
|
||||
They are all stringified, so be sure to parse them accordingly by creating a script, if necessary.
|
||||
|
||||
### Command line options
|
||||
##### `--template glob [--template glob2 [...]]` (required) (alias: -t)
|
||||
A glob pattern of template files to load.
|
||||
|
||||
A template file may be of any type and extension, and supports [Handlebars](https://handlebarsjs.com) as a parsing engine for the file names and contents, so you may customize both with variables from your configuration.
|
||||
|
||||
You can load more than one template list by simple adding more `--template` arguments.
|
||||
|
||||
##### `--output path` (optional) (alias -o)
|
||||
The output directory to put the new files in. They will attempt to maintain their regular structure as they are found, if possible.
|
||||
|
||||
Your new scaffold will be placed under a directory with the scaffold name from the argumemts.
|
||||
|
||||
You may also pass a function to transform the output path for each file individually.
|
||||
This function takes 2 arguments: filename, and base glob path
|
||||
|
||||
##### `--locals key=value [--locals key=value [,...]]` (optional) (alias: -l)
|
||||
Pass a KV map to the template for parsing.
|
||||
|
||||
##### `--create-sub-folder [true|false]` (optional) (alias -S) (default: true)
|
||||
Whether to create a subfolder for the output with all the files inside, or simply dump them directly in the output folder.
|
||||
|
||||
#####
|
||||
|
||||
### Use in Node.js
|
||||
You can also build the scaffold yourself, if you want to create more complex arguments or scaffold groups.
|
||||
Simply pass a config object to the constructor, and invoke `run()` when you are ready to start.
|
||||
The config takes similar arguments to the command line:
|
||||
|
||||
```javascript
|
||||
const SimpleScaffold = require('simple-scaffold').default
|
||||
|
||||
const scaffold = new SimpleScaffold({
|
||||
name: 'component',
|
||||
templates: [path.join(__dirname, 'scaffolds', 'component')],
|
||||
output: path.join(__dirname, 'src', 'components'),
|
||||
createSubFolder: true,
|
||||
locals: {
|
||||
property: 'value',
|
||||
}
|
||||
}).run()
|
||||
```
|
||||
|
||||
## Example Scaffold Input
|
||||
|
||||
### Input Directory structure
|
||||
```
|
||||
- project
|
||||
- scaffold
|
||||
- {{Name}}.js
|
||||
- src
|
||||
- components
|
||||
- ...
|
||||
```
|
||||
|
||||
#### project/scaffold/{{Name}}.js
|
||||
```js
|
||||
const React = require('react')
|
||||
|
||||
module.exports = class {{Name}} extends React.Component {
|
||||
render() {
|
||||
<div className="{{className}}">{{Name}} Component</div>
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Run Example
|
||||
```bash
|
||||
simple-scaffold MyComponent \
|
||||
--template project/scaffold/**/* \
|
||||
--output src/components \
|
||||
--locals 'className=my-component`
|
||||
```
|
||||
|
||||
## Example Scaffold Output
|
||||
#### Directory structure
|
||||
```
|
||||
- project
|
||||
- src
|
||||
- components
|
||||
- MyComponent
|
||||
- MyComponent.js
|
||||
- ...
|
||||
```
|
||||
|
||||
With `createSubfolder = false`:
|
||||
```
|
||||
- project
|
||||
- src
|
||||
- components
|
||||
- MyComponent.js
|
||||
- ...
|
||||
```
|
||||
|
||||
#### project/scaffold/MyComponent/MyComponent.js
|
||||
```js
|
||||
const React = require('react')
|
||||
|
||||
module.exports = class MyComponent extends React.Component {
|
||||
render() {
|
||||
<div className="my-component">MyComponent Component</div>
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
55
cmd.ts
Normal file
55
cmd.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import SimpleScaffold from './scaffold'
|
||||
import * as fs from 'fs'
|
||||
import IScaffold from './index'
|
||||
import * as cliArgs from 'command-line-args'
|
||||
import * as cliUsage from 'command-line-usage'
|
||||
import * as path from 'path'
|
||||
|
||||
type Def = cliArgs.OptionDefinition & { description?: string }
|
||||
|
||||
function localsParser(content: string) {
|
||||
const [key, value] = content.split('=')
|
||||
return { [key]: value }
|
||||
}
|
||||
|
||||
function filePathParser(content: string) {
|
||||
if (content.startsWith('/')) {
|
||||
return content
|
||||
}
|
||||
return [process.cwd(), content].join(path.sep)
|
||||
}
|
||||
|
||||
const defs: Def[] = [
|
||||
{ name: 'name', alias: 'n', type: String, description: 'Component output name', defaultOption: true },
|
||||
{ name: 'templates', alias: 't', type: filePathParser, multiple: true },
|
||||
{ name: 'output', alias: 'o', type: filePathParser, multiple: true },
|
||||
{ name: 'locals', alias: 'l', multiple: true, type: localsParser },
|
||||
{ name: 'create-sub-folder', alias: 'S', type: (text: string) => text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true },
|
||||
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this help message' },
|
||||
]
|
||||
|
||||
const args = cliArgs(defs, { camelCase: true })
|
||||
|
||||
const help = [
|
||||
{ header: 'Scaffold Generator', content: 'Generate scaffolds for your project based on file templates.' },
|
||||
{ header: 'Options', optionList: defs }
|
||||
]
|
||||
|
||||
args.locals = (args.locals || []).reduce((all: object, cur: object) => ({ ...all, ...cur }), {} as IScaffold.Config['locals'])
|
||||
if (args.createSubFolder === null) {
|
||||
args.createSubFolder = true
|
||||
}
|
||||
console.info('Config:', args)
|
||||
|
||||
if (args.help || !args.name) {
|
||||
console.log(cliUsage(help))
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
new SimpleScaffold({
|
||||
name: args.name,
|
||||
templates: args.templates,
|
||||
output: args.output,
|
||||
locals: args.locals,
|
||||
createSubfolder: args.createSubFolder,
|
||||
}).run()
|
||||
292
dist/cmd.js
vendored
Executable file
292
dist/cmd.js
vendored
Executable file
@@ -0,0 +1,292 @@
|
||||
#!/usr/bin/env node
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(typeof self !== 'undefined' ? self : this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 6);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ }),
|
||||
/* 5 */,
|
||||
/* 6 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var scaffold_1 = __webpack_require__(1);
|
||||
var cliArgs = __webpack_require__(7);
|
||||
var cliUsage = __webpack_require__(8);
|
||||
var path = __webpack_require__(0);
|
||||
function localsParser(content) {
|
||||
var _a;
|
||||
var _b = content.split('='), key = _b[0], value = _b[1];
|
||||
return _a = {}, _a[key] = value, _a;
|
||||
}
|
||||
function filePathParser(content) {
|
||||
if (content.startsWith('/')) {
|
||||
return content;
|
||||
}
|
||||
return [process.cwd(), content].join(path.sep);
|
||||
}
|
||||
var defs = [
|
||||
{ name: 'name', alias: 'n', type: String, description: 'Component output name', defaultOption: true },
|
||||
{ name: 'templates', alias: 't', type: filePathParser, multiple: true },
|
||||
{ name: 'output', alias: 'o', type: filePathParser, multiple: true },
|
||||
{ name: 'locals', alias: 'l', multiple: true, type: localsParser },
|
||||
{ name: 'create-sub-folder', alias: 'S', type: function (text) { return text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true; } },
|
||||
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this help message' },
|
||||
];
|
||||
var args = cliArgs(defs, { camelCase: true });
|
||||
var help = [
|
||||
{ header: 'Scaffold Generator', content: 'Generate scaffolds for your project based on file templates.' },
|
||||
{ header: 'Options', optionList: defs }
|
||||
];
|
||||
args.locals = (args.locals || []).reduce(function (all, cur) { return (__assign({}, all, cur)); }, {});
|
||||
if (args.createSubFolder === null) {
|
||||
args.createSubFolder = true;
|
||||
}
|
||||
console.info('Config:', args);
|
||||
if (args.help || !args.name) {
|
||||
console.log(cliUsage(help));
|
||||
process.exit(0);
|
||||
}
|
||||
new scaffold_1.default({
|
||||
name: args.name,
|
||||
templates: args.templates,
|
||||
output: args.output,
|
||||
locals: args.locals,
|
||||
createSubfolder: args.createSubFolder,
|
||||
}).run();
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 7 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("command-line-args");
|
||||
|
||||
/***/ }),
|
||||
/* 8 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("command-line-usage");
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=cmd.js.map
|
||||
1
dist/cmd.js.map
vendored
Executable file
1
dist/cmd.js.map
vendored
Executable file
File diff suppressed because one or more lines are too long
1
dist/dist/cmd.d.ts
vendored
Executable file
1
dist/dist/cmd.d.ts
vendored
Executable file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
13
dist/dist/scaffold.d.ts
vendored
Normal file → Executable file
13
dist/dist/scaffold.d.ts
vendored
Normal file → Executable file
@@ -0,0 +1,13 @@
|
||||
import IScaffold from './index';
|
||||
declare class SimpleScaffold {
|
||||
config: IScaffold.Config;
|
||||
locals: IScaffold.Config['locals'];
|
||||
constructor(config: IScaffold.Config);
|
||||
private parseLocals;
|
||||
private fileList;
|
||||
private getFileContents;
|
||||
private getOutputPath;
|
||||
private writeFile;
|
||||
run(): void;
|
||||
}
|
||||
export default SimpleScaffold;
|
||||
|
||||
1
dist/dist/test.d.ts
vendored
Executable file
1
dist/dist/test.d.ts
vendored
Executable file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
96
dist/main.js
vendored
Executable file
96
dist/main.js
vendored
Executable file
@@ -0,0 +1,96 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 7);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ({
|
||||
|
||||
/***/ 7:
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
(function webpackMissingModule() { throw new Error("Cannot find module \"add\""); }());
|
||||
module.exports = __webpack_require__(8);
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 8:
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("jest");
|
||||
|
||||
/***/ })
|
||||
|
||||
/******/ });
|
||||
});
|
||||
//# sourceMappingURL=main.js.map
|
||||
1
dist/main.js.map
vendored
Executable file
1
dist/main.js.map
vendored
Executable file
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["webpack/universalModuleDefinition","webpack/bootstrap c994c2c400fa1fc61abe","external \"jest\""],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;ACVA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAK;AACL;AACA;;AAEA;AACA;AACA;AACA,mCAA2B,0BAA0B,EAAE;AACvD,yCAAiC,eAAe;AAChD;AACA;AACA;;AAEA;AACA,8DAAsD,+DAA+D;;AAErH;AACA;;AAEA;AACA;;;;;;;;;;;;;;;;;AC7DA,iC","file":"main.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 7);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap c994c2c400fa1fc61abe","module.exports = require(\"jest\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"jest\"\n// module id = 8\n// module chunks = 3"],"sourceRoot":""}
|
||||
218
dist/scaffold.js
vendored
218
dist/scaffold.js
vendored
@@ -1 +1,217 @@
|
||||
!function(t){function e(n){if(o[n])return o[n].exports;var i=o[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var o={};e.m=t,e.c=o,e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=o(1),i=(o.n(n),o(2)),s=(o.n(i),function(){function t(t){if(this.locals={},this.scaffoldName=process.argv[2],this.DefaultConfig={templates:[],output:i.resolve(process.cwd()),locals:{Name:this.scaffoldName[0].toUpperCase()+this.scaffoldName.slice(1),name:this.scaffoldName[0].toLowerCase()+this.scaffoldName.slice(1)}},!this.scaffoldName)throw new Error("Must provide scaffold name");this.config=Object.assign({},this.DefaultConfig,t),this.locals=Object.assign({},this.DefaultConfig.locals,t.locals),console.info("Config loaded:",this.config),console.info("Locals:",this.locals)}return t.prototype.parseLocals=function(t){var e=this,o=t.toString(),n=/{[%]\s*([^%{}]+)\s*[%]}/gi;return o.replace(n,function(t,o){return e.locals[o]})},t.prototype.getFileList=function(t){var e=this,o=[];return t.forEach(function(t){var s=n.lstatSync(t);if(s.isFile())console.info("pushing",t),o.push(t);else if(s.isDirectory()){console.info("going into dir",t);var r=n.readdirSync(t).map(function(e){return i.join(t,e)});o=o.concat(e.getFileList(r))}}),o},t.prototype.getFileContents=function(t){return n.readFileSync(t).toString()},t.prototype.getOutputPath=function(t){var e;return e="function"==typeof this.config.output?this.config.output(t):this.config.output+"/"+this.scaffoldName+"/"+i.basename(t),this.parseLocals(e)},t.prototype.writeFile=function(t,e){n.existsSync(i.dirname(t))||n.mkdirSync(i.dirname(t)),console.info("Writing file:",t),n.writeFileSync(t,e,{encoding:"utf-8"})},t.prototype.run=function(){var t=this,e=this.getFileList(this.config.templates);console.info(e),e.forEach(function(e){var o=t.getOutputPath(e),n=t.getFileContents(e),i=t.parseLocals(n);t.writeFile(o,i)})},t}());exports.default=s},function(t,e){t.exports=require("fs")},function(t,e){t.exports=require("path")}]);
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(typeof self !== 'undefined' ? self : this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 1);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=scaffold.js.map
|
||||
1
dist/scaffold.js.map
vendored
Executable file
1
dist/scaffold.js.map
vendored
Executable file
File diff suppressed because one or more lines are too long
246
dist/test.js
vendored
Executable file
246
dist/test.js
vendored
Executable file
@@ -0,0 +1,246 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(typeof self !== 'undefined' ? self : this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 5);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ }),
|
||||
/* 5 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var scaffold_1 = __webpack_require__(1);
|
||||
var path = __webpack_require__(0);
|
||||
var templateDir = path.join(process.cwd(), 'examples');
|
||||
new scaffold_1.default({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output/no-create-subpath',
|
||||
createSubfolder: false,
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run();
|
||||
new scaffold_1.default({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output',
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run();
|
||||
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=test.js.map
|
||||
1
dist/test.js.map
vendored
Executable file
1
dist/test.js.map
vendored
Executable file
File diff suppressed because one or more lines are too long
0
examples/test-input/Component/.hidden-file
Normal file
0
examples/test-input/Component/.hidden-file
Normal file
@@ -1,13 +0,0 @@
|
||||
import * as React from 'react'
|
||||
|
||||
class {%Name%} extends React.Component {
|
||||
private {%property%}
|
||||
|
||||
constructor() {
|
||||
this.{%property%} = {%value%}
|
||||
}
|
||||
|
||||
<div className={css.{%Name%}} />
|
||||
}
|
||||
|
||||
export default {%Name%}
|
||||
19
examples/test-input/Component/{{Name}}.tsx
Normal file
19
examples/test-input/Component/{{Name}}.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import * as React from 'react'
|
||||
import * as css from './{{Name}}.css'
|
||||
|
||||
class {{Name}} extends React.Component<any> {
|
||||
private {{property}}
|
||||
|
||||
constructor(props: any) {
|
||||
super(props)
|
||||
this.{{property}} = {{value}}
|
||||
}
|
||||
|
||||
public render() {
|
||||
return (
|
||||
<div className={ css.{{Name}} } />
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default {{Name}}
|
||||
0
examples/test-output/.gitkeep
Normal file
0
examples/test-output/.gitkeep
Normal file
22
index.d.ts
vendored
22
index.d.ts
vendored
@@ -1,15 +1,21 @@
|
||||
export namespace IScaffold {
|
||||
declare namespace IScaffold {
|
||||
|
||||
export interface IConfig {
|
||||
export interface Config {
|
||||
name?: string
|
||||
templates: string[]
|
||||
output: string | ((path: string) => string)
|
||||
locals?: any
|
||||
output: string | ((path: string, base: string) => string)
|
||||
locals?: Locals
|
||||
createSubfolder?: boolean
|
||||
}
|
||||
|
||||
export interface IReplacement {
|
||||
find: string | RegExp
|
||||
replace(): string
|
||||
[other: string]: any
|
||||
export interface Locals {
|
||||
[k: string]: string
|
||||
}
|
||||
|
||||
export interface FileRepr {
|
||||
base: string
|
||||
file: string
|
||||
}
|
||||
}
|
||||
|
||||
export default IScaffold
|
||||
|
||||
43
package.json
43
package.json
@@ -1,26 +1,45 @@
|
||||
{
|
||||
"name": "simple-scaffold",
|
||||
"version": "0.1.2",
|
||||
"version": "0.4.3",
|
||||
"description": "Create files based on templates",
|
||||
"repository": "https://github.com/chenasraf/simple-scaffold.git",
|
||||
"author": "Chen Asraf <inbox@casraf.com>",
|
||||
"license": "MIT",
|
||||
"main": "./dist/scaffold.js",
|
||||
"bin": "./dist/scaffold.js",
|
||||
"main": "dist/scaffold.js",
|
||||
"bin": "dist/cmd.js",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"build": "webpack -p && chmod +x ./dist/scaffold.js",
|
||||
"build": "NODE_ENV=${NODE_ENV:-production} webpack -p && chmod -R +x ./dist",
|
||||
"dev": "webpack --watch",
|
||||
"start": "node ./dist/scaffold.js",
|
||||
"test": "yarn build && node ./scripts/test.js"
|
||||
"start": "node dist/scaffold.js",
|
||||
"test": "jest",
|
||||
"cmd": "dist/cmd.js",
|
||||
"build-test": "yarn build && yarn test",
|
||||
"build-cmd": "yarn build && yarn cmd"
|
||||
},
|
||||
"files": [
|
||||
"./dist/scaffold.js"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/node": "^8.0.50",
|
||||
"dependencies": {
|
||||
"command-line-args": "^5.0.2",
|
||||
"command-line-usage": "^5.0.5",
|
||||
"glob": "^7.1.2",
|
||||
"handlebars": "^4.0.11",
|
||||
"ts-loader": "^3.1.1",
|
||||
"typescript": "^2.6.1",
|
||||
"webpack": "^3.8.1",
|
||||
"webpack-dev-server": "^2.9.4"
|
||||
"webpack-dev-server": "^2.9.4",
|
||||
"webpack-node-externals": "^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/command-line-args": "^5.0.0",
|
||||
"@types/command-line-usage": "^5.0.1",
|
||||
"@types/glob": "^5.0.34",
|
||||
"@types/handlebars": "^4.0.36",
|
||||
"@types/node": "^8.0.50",
|
||||
"jest": "^22.0.4"
|
||||
},
|
||||
"jest": {
|
||||
"testPathIgnorePatterns": [
|
||||
"node_modules",
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
122
scaffold.ts
122
scaffold.ts
@@ -1,65 +1,73 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { IScaffold } from './index'
|
||||
import IScaffold from './index'
|
||||
import * as glob from 'glob'
|
||||
import * as handlebars from 'handlebars'
|
||||
|
||||
class SimpleScaffold {
|
||||
private config: IScaffold.IConfig
|
||||
private locals = {} as any
|
||||
public scaffoldName = process.argv[2]
|
||||
private DefaultConfig: IScaffold.IConfig = {
|
||||
templates: [],
|
||||
output: path.resolve(process.cwd()),
|
||||
locals: {
|
||||
Name: this.scaffoldName[0].toUpperCase() + this.scaffoldName.slice(1),
|
||||
name: this.scaffoldName[0].toLowerCase() + this.scaffoldName.slice(1)
|
||||
}
|
||||
}
|
||||
public config: IScaffold.Config
|
||||
public locals: IScaffold.Config['locals'] = {} as any
|
||||
|
||||
constructor(config: IScaffold.IConfig) {
|
||||
if (!this.scaffoldName) {
|
||||
throw new Error('Must provide scaffold name')
|
||||
constructor(config: IScaffold.Config) {
|
||||
const DefaultConfig: IScaffold.Config = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
}
|
||||
this.config = (Object as any).assign({}, this.DefaultConfig, config)
|
||||
this.locals = (Object as any).assign({}, this.DefaultConfig.locals, config.locals)
|
||||
console.info('Config loaded:', this.config)
|
||||
console.info('Locals:', this.locals)
|
||||
|
||||
this.config = { ...DefaultConfig, ...config }
|
||||
|
||||
const DefaultLocals = {
|
||||
Name: this.config.name![0].toUpperCase() + this.config.name!.slice(1),
|
||||
name: this.config.name![0].toLowerCase() + this.config.name!.slice(1)
|
||||
}
|
||||
|
||||
this.locals = { ...DefaultLocals, ...config.locals }
|
||||
}
|
||||
|
||||
private parseLocals(text: string): string {
|
||||
let out = text.toString()
|
||||
const pattern = /{[%]\s*([^%{}]+)\s*[%]}/gi
|
||||
return out.replace(pattern, (match: string, $1: string) => this.locals[$1])
|
||||
const template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
})
|
||||
return template(this.locals)
|
||||
}
|
||||
|
||||
private getFileList(pathList: string[]): string[] {
|
||||
let outList: string[] = []
|
||||
|
||||
pathList.forEach((checkPath: string) => {
|
||||
const stat = fs.lstatSync(checkPath)
|
||||
if (stat.isFile()) {
|
||||
console.info('pushing', checkPath)
|
||||
outList.push(checkPath)
|
||||
} else if (stat.isDirectory()) {
|
||||
console.info('going into dir', checkPath)
|
||||
const innerFiles = fs.readdirSync(checkPath).map(p => path.join(checkPath, p))
|
||||
outList = outList.concat(this.getFileList(innerFiles))
|
||||
private fileList(input: string[]): IScaffold.FileRepr[] {
|
||||
const output: IScaffold.FileRepr[] = []
|
||||
for (const checkPath of input) {
|
||||
const files = glob.sync(checkPath, { dot: true })
|
||||
.map(g => g[0] == '/' ? g : path.join(process.cwd(), g))
|
||||
const idx = checkPath.indexOf('*')
|
||||
let cleanCheckPath = checkPath
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1)
|
||||
}
|
||||
})
|
||||
|
||||
return outList
|
||||
for (const file of files) {
|
||||
output.push({ base: cleanCheckPath, file })
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
private getFileContents(filePath: string): string {
|
||||
console.log(fs.readFileSync(filePath))
|
||||
return fs.readFileSync(filePath).toString()
|
||||
}
|
||||
|
||||
private getOutputPath(file: string): string {
|
||||
let out
|
||||
private getOutputPath(file: string, basePath: string): string {
|
||||
let out: string
|
||||
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file)
|
||||
out = this.config.output(file, basePath)
|
||||
} else {
|
||||
out = this.config.output + `/${this.scaffoldName}/` + path.basename(file)
|
||||
const outputDir = this.config.output + (this.config.createSubfolder ? `/${this.config.name}/` : '/')
|
||||
const idx = file.indexOf(basePath)
|
||||
let relativeFilePath = file
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1)
|
||||
}
|
||||
out = outputDir + relativeFilePath
|
||||
}
|
||||
|
||||
return this.parseLocals(out)
|
||||
@@ -70,19 +78,35 @@ class SimpleScaffold {
|
||||
fs.mkdirSync(path.dirname(filePath))
|
||||
}
|
||||
console.info('Writing file:', filePath)
|
||||
fs.writeFileSync(filePath, fileContents, { encoding: 'utf-8' })
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, (err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public run() {
|
||||
const inputFiles = this.getFileList(this.config.templates)
|
||||
console.info(inputFiles)
|
||||
inputFiles.forEach((file: string) => {
|
||||
const outputPath = this.getOutputPath(file)
|
||||
public run(): void {
|
||||
console.log(`Generating scaffold: ${this.config.name}...`)
|
||||
const templates = this.fileList(this.config.templates)
|
||||
|
||||
let fileConf, count = 0
|
||||
for (fileConf of templates) {
|
||||
count++
|
||||
const { file, base } = fileConf
|
||||
const outputPath = this.getOutputPath(file, base)
|
||||
const contents = this.getFileContents(file)
|
||||
const outputContents = this.parseLocals(contents)
|
||||
|
||||
this.writeFile(outputPath, outputContents)
|
||||
})
|
||||
console.info('Parsing:', { file, base, outputPath, outputContents: outputContents.replace("\n", "\\n") })
|
||||
}
|
||||
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!')
|
||||
}
|
||||
|
||||
console.log('Done')
|
||||
}
|
||||
}
|
||||
|
||||
exports.default = SimpleScaffold
|
||||
export default SimpleScaffold
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
const Scaffolder = require('../dist/scaffold').default
|
||||
|
||||
const templateDir = process.cwd() + '/examples'
|
||||
|
||||
const scf = new Scaffolder({
|
||||
templates: [templateDir + '/test-input/Component'],
|
||||
output: templateDir + '/test-output',
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run()
|
||||
23
test.ts
Normal file
23
test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import SimpleScaffold from './scaffold'
|
||||
import * as path from 'path'
|
||||
|
||||
const templateDir = path.join(process.cwd(), 'examples')
|
||||
|
||||
new SimpleScaffold({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output/no-create-subpath',
|
||||
createSubfolder: false,
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run()
|
||||
|
||||
new SimpleScaffold({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output',
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run()
|
||||
@@ -1,9 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "es2015",
|
||||
"module": "commonjs",
|
||||
"lib": ["es2017", "es6"],
|
||||
"declaration": true,
|
||||
"outDir": "./dist",
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
|
||||
@@ -1,22 +1,43 @@
|
||||
const path = require('path')
|
||||
const webpack = require('webpack')
|
||||
const nodeExternals = require('webpack-node-externals')
|
||||
|
||||
module.exports = {
|
||||
devtool: process.env.NODE_ENV === 'develop' ? 'inline-source-map' : 'source-map',
|
||||
target: 'node',
|
||||
entry: './scaffold.ts',
|
||||
entry: {
|
||||
scaffold: './scaffold.ts',
|
||||
test: './test.ts',
|
||||
cmd: './cmd.ts',
|
||||
},
|
||||
output: {
|
||||
filename: 'scaffold.js',
|
||||
path: path.resolve(__dirname, 'dist')
|
||||
filename: '[name].js',
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
|
||||
library: 'library',
|
||||
libraryTarget: 'umd',
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts']
|
||||
},
|
||||
externals: [nodeExternals()],
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
loader: 'ts-loader',
|
||||
exclude: ['./examples']
|
||||
exclude: ['./examples', 'node_modules']
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
'__dirname': '__dirname'
|
||||
}),
|
||||
new webpack.BannerPlugin({
|
||||
banner: '#!/usr/bin/env node',
|
||||
raw: true,
|
||||
include: /cmd\.js/,
|
||||
})
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user