Skip to content

Commit

Permalink
Create method for extracting config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
itslenny committed Apr 23, 2017
1 parent 3162458 commit 88839a0
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
**/*.js
**/*.js.map
test/output
test/data/projects/**/tmp
node_modules
**/.DS_Store
**/*.log
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ docs comming...

## TODO

* Add list comment (list available scaffolds)
* Add comments
* Add more tests
* Break up documentation into separate pages
* Add more examples / animated gifs
* Create scaff scaff
* Create test-project scaff
* Split scaffs-cli / scaffs (lib)
* Split scaffs-cli / scaffs (lib)
* Refactor to Async/Await
9 changes: 5 additions & 4 deletions bin/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ function parseInputData(data: any): Object {
}

function getScaffoldVariables(config: ScaffoldConfig, data: TemplateOptionsData): TemplateOptionsData {
let variables = Scaffolder.getScaffoldVariables(config);
let variablesResult: TemplateOptionsData = {};
let variables = config.variables;

for (let i = 0, len = variables.length; i < len; i++) {
let variable = variables[i];
let variableName = typeof variable === 'object' ? variable.name : variable;
let variableOptional = typeof variable === 'object' ? variable.optional : false;
let variableName = variable.name;
let variableOptional = variable.optional;
if (data[variableName]) {
variablesResult[variableName] = data[variableName];
} else {
let variablePrompt = typeof variable === 'object' ? (variable.prompt || variableName) : variableName;
let variablePrompt = variable.prompt;
variablesResult[variableName] = prompt(`${variablePrompt}: `);
}
if (!variableOptional && !variablesResult[variableName]) {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"name": "scaffs-cli",
"version": "0.0.4",
"version": "0.0.5",
"description": "A simple language agnostic scaffolder",
"typings": "./src/index.d.ts",
"main": "./src/index.js",
"scripts": {
"start": "tsc -w",
"test": "node test/test-runner.js",
"build": "tsc",
"clean": "rimraf \"@(bin|src|test)/**/@(*.js|*.d.ts|*.js.map)\" && npm run clean:test-output",
"clean:test-output": "rimraf test/output",
"lint": "tslint \"**/*.ts\" --type-check --project=tsconfig.json --exclude=node_modules --exclude=**/*.d.ts --exclude=test/data/**/*.* --exclude=test/output/**/*.*",
"lint": "tslint \"**/*.ts\" --exclude=node_modules --exclude=**/*.d.ts --exclude=test/data/**/*.* --exclude=test/output/**/*.*",
"tslint": "tslint"
},
"repository": {
Expand Down
21 changes: 21 additions & 0 deletions src/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Lenny Urbanowski

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.
2 changes: 2 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Scaffs

14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { ScaffoldTemplater } from './lib/scaffold-templater';
import { ScaffsConfigLoader } from './lib/scaffs-config-loader';
import { TemplateOptions } from './contracts/template-options';
import { ScaffsConfig } from './contracts/scaffs-config';
import { ScaffoldConfig } from './contracts/scaffold';
import { ScaffoldConfig, ScaffoldVariableConfig } from './contracts/scaffold';

export * from './contracts/scaffold';
export * from './contracts/scaffs-config';
export * from './contracts/template-options';

export module Scaffolder {

Expand Down Expand Up @@ -78,6 +82,14 @@ export module Scaffolder {
.then(ScaffsConfigLoader.resolveScaffolds);
}

export function getScaffoldVariables(scaffoldConfig: ScaffoldConfig): ScaffoldVariableConfig[] {
return ScaffoldLoader.getScaffoldVariables(scaffoldConfig);
}

export function getScaffoldVariablesFromPath(scaffoldPath: string): Promise<ScaffoldVariableConfig[]> {
return ScaffoldLoader.getScaffoldVariablesFromPath(scaffoldPath);
}

export function getScaffoldPath(config: ScaffsConfig, scaffoldName: string): string {
return config.absoluteScaffPaths && config.absoluteScaffPaths[scaffoldName];
}
Expand Down
23 changes: 22 additions & 1 deletion src/lib/scaffold-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';

import { FileUtils } from './file-utils';
import { Scaffold, FileDataNode, FileDataNodeType, ScaffoldConfig } from '../contracts/scaffold';
import { Scaffold, FileDataNode, FileDataNodeType, ScaffoldConfig, ScaffoldVariableConfig } from '../contracts/scaffold';

const SCAFFOLD_CONFIG_FILE = '.scaffold.json';

Expand Down Expand Up @@ -52,6 +52,27 @@ export module ScaffoldLoader {
});
}

/**
* Gets the list of scaffold variables from the specified path
*
* @param scaffoldPath - path to scaffold to load
*/
export function getScaffoldVariablesFromPath(scaffoldPath: string): Promise<ScaffoldVariableConfig[]> {
return loadScaffoldConfig(scaffoldPath).then(scaffoldConfig => getScaffoldVariables(scaffoldConfig));
}

/**
* Gets the list of scaffold variables from the specified path
*
* @param scaffoldPath - path to scaffold to load
*/
export function getScaffoldVariables(scaffoldConfig: ScaffoldConfig): ScaffoldVariableConfig[] {
return scaffoldConfig.variables.map(variable => ({
name: typeof variable === 'object' ? variable.name : variable,
prompt: typeof variable === 'object' ? (variable.prompt || variable.name) : variable,
optional: typeof variable === 'object' ? !!variable.optional : false,
}));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/scaffs-config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export module ScaffsConfigLoader {
try {
let config = fs.readJsonSync(path.join(projectRoot, SCAFFS_CONFIG_FILE));
if (!config.baseConfigPath) {
config.baseConfigPath = path.dirname(projectRoot);
config.baseConfigPath = projectRoot;
}
resolve(config);

Expand Down
21 changes: 21 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "scaffs",
"version": "0.0.4",
"description": "A simple language agnostic scaffolder",
"typings": "./index.d.ts",
"main": "./index.js",
"repository": {
"type": "git",
"url": "https://github.com/itslenny/scaffs.git"
},
"author": "Lenny Urbanowski",
"license": "MIT",
"engineStrict": true,
"engines": {
"node": ">=5.0.0"
},
"dependencies": {
"fs-extra": "^2.1.2",
"lodash": "^4.17.4"
}
}
3 changes: 0 additions & 3 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
140
],
"no-arg": true,
"no-boolean-literal-compare": true,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": true,
"no-construct": true,
Expand All @@ -30,12 +29,10 @@

"no-empty": false,
"no-eval": true,
"no-for-in-array": true,
"no-irregular-whitespace": true,
"no-string-literal": false,
"no-trailing-whitespace": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"object-literal-key-quotes": [
true,
Expand Down

0 comments on commit 88839a0

Please sign in to comment.