Skip to content

Commit

Permalink
Merge pull request #9 from zweitag/generate-selectors
Browse files Browse the repository at this point in the history
Generate selectors
  • Loading branch information
Alex Kott authored Jun 4, 2019
2 parents 85f4896 + 53f1014 commit aba06e7
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 57 deletions.
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
*-debug.log
*-error.log
.oclif.manifest.json
/.nyc_output
/dist
/lib
/tmp
node_modules
public

/lib
/.zwen
/test
60 changes: 28 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"chalk": "^2.4.2",
"constant-case": "^2.0.0",
"ejs": "^2.6.1",
"fs-readdir-recursive": "^1.1.0",
"fuzzy": "^0.1.3",
"inquirer-autocomplete-prompt": "^1.0.1",
"minimist": "^1.2.0",
"pascal-case": "^2.0.1",
"yeoman-environment": "^2.3.4",
Expand Down
4 changes: 4 additions & 0 deletions src/constants/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const selectExportsAll = new RegExp(
'export \\*(.+?(?=export \\*|$))',
'gs',
);
export const selectExportConstNames = new RegExp(
'(?<=\nexport const )\\w*',
'gs',
);
export const selectDefaultImports = new RegExp(
'import \\w(.+?(?=import|$|\\n\\n))',
'gs',
Expand Down
16 changes: 10 additions & 6 deletions src/constants/templateStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ export const importAllAsFrom = (as: string, from: string) =>
export const importNamedFrom = (name: string, from: string) =>
`import { ${name} } from '${from}';\n`;

// ACTIONS
export const creatorTestHead = (path: string) =>
`import * as t from '@/actions/types';\n` +
`import * as actions from './creators';\n\n` +
`describe('actions/${path}', () => {\n`;
export const describeTestStart = (path: string) =>
`describe('${path}', () => {\n`;

export const creatorTestFoot = () => '});\n';
export const describeTestEnd = () =>
`});\n`;

// REDUCERS
export const exportDefaultCombineReducers = () =>
'export default combineReducers({\n';

// SELECTORS
export const exportAllFromReducers = () =>
`/* zwen-keep-start */\n` +
`export * from '@/reducers';\n` +
`/* zwen-keep-end */\n\n`;
17 changes: 10 additions & 7 deletions src/generators/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export default class ActionGenerator extends Generator implements Zwenerator {

async prompting() {
const answer = await this.prompt({
message: 'Create an action type with the same name?',
name: 'withActionType',
type: 'confirm',
name: 'withActionType',
message: 'Create an action type with the same name?',
});

this.withActionType = answer.withActionType;
Expand Down Expand Up @@ -80,12 +80,12 @@ export default class ActionGenerator extends Generator implements Zwenerator {
}

addActionCreator() {
const importStatement = t.importAllAsFrom('t', '@/actions/types') + '\n';
const creatorTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/creator.ejs`));
const newCreator = ejs.render(creatorTemplate, this.templateConfig);

// read
const creatorsFile = this.fs.read(`${this.absolutePath}/creators.js`, { defaults: importStatement });
const fileHead = t.importAllAsFrom('t', '@/actions/types') + '\n';
const creatorsFile = this.fs.read(`${this.absolutePath}/creators.js`, { defaults: fileHead });
// update
const updateOptions = {
separator: '\n\n',
Expand All @@ -98,13 +98,16 @@ export default class ActionGenerator extends Generator implements Zwenerator {
addActionCreatorTest() {
const testTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/creator.test.ejs`));
const newTest = ejs.render(testTemplate, this.templateConfig);
const fileDefaults = t.creatorTestHead(this.destPath);

// read
const testFile = this.fs.read(`${this.absolutePath}/creators.test.js`, { defaults: fileDefaults });
const fileHead =
t.importAllAsFrom('t', '@/actions/types') +
t.importAllAsFrom('actions', './creators') + '\n' +
t.describeTestStart(`actions/${this.destPath}`);
const testFile = this.fs.read(`${this.absolutePath}/creators.test.js`, { defaults: fileHead });
// update
const updateOptions = {
appendixIfNew: t.creatorTestFoot(),
appendixIfNew: t.describeTestEnd(),
replaceStringSeparator: ' ',
separator: '\n\n ',
};
Expand Down
1 change: 0 additions & 1 deletion src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export default class ComponentGenerator extends Generator implements Zwenerator
}

writing() {
const destPath = `${this.absolutePath}`;
const templateName = this.classComp ? 'classComp' : 'component';
const componentName = this.fileName.toPascalCase();
this.fs.copyTpl(
Expand Down
1 change: 1 addition & 0 deletions src/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const registeredGenerators = [
'component',
'middleware',
'reducer',
'selector',
];
4 changes: 2 additions & 2 deletions src/generators/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export default class ReducerGenerator extends Generator implements Zwenerator {

destDirWithFileName.forEach((subPath: string) => {
// read
const importWithNewLine = t.importNamedFrom('combineReducers', 'redux') + '\n';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: importWithNewLine });
const fileHead = t.importNamedFrom('combineReducers', 'redux') + '\n';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: fileHead });
// update imports
let updatedFile = addToFile(file, t.importDefaultFrom(subPath, `./${subPath}`), r.selectDefaultImports);

Expand Down
Loading

0 comments on commit aba06e7

Please sign in to comment.