Skip to content

Commit

Permalink
feat(Core,Cli): Better path-prefix support
Browse files Browse the repository at this point in the history
Entering non exitsting path-prefix will now result with scaffolder
notifying the user about the missing paths and creating them
automaticlly

Closes #33
  • Loading branch information
galElmalah committed Nov 13, 2020
1 parent 0e807f0 commit 3424e15
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
'rules': {
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-var-requires':'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'indent': [
'error',
Expand Down
1 change: 0 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
"packages/*"
],
"version": "independent"

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"bootstrap": "lerna bootstrap --hoist",
"build": "tsc -b packages/scaffolder-cli packages/scaffolder-core",
"test": "lerna run test",
"test": "npm run build && lerna run test --stream",
"version": "lerna version --conventional-commits",
"publish": "lerna publish from-package",
"lint": "eslint packages/scaffolder-cli packages/scaffolder-core",
Expand Down
1 change: 1 addition & 0 deletions packages/scaffolder-cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const baseJestConfig = require('../../jest.base.config.js');

module.exports = {
...baseJestConfig
};
7 changes: 7 additions & 0 deletions packages/scaffolder-cli/tests/e2e/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ describe('e2e', () => {
).toBeTruthy();
});

it('should create the missing directories in the prefix-path', () => {
execOnTestDir(
'create not-nested key1=awesome key5=awesome --folder not-nested --path-prefix prefix/not-existing'
);
expect(isFolderExists('prefix/not-existing/not-nested')).toBeTruthy();
});

it('should create the template with the right values as keys', () => {
execOnTestDir(
'create not-nested key1=awesome key5=awesome --folder not-nested'
Expand Down

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as fs from 'fs';

export const createMissingFoldersInPath = (basePath: string, toPath: string) => {
const missingFoldersPaths = [];
if (!toPath) {
return missingFoldersPaths;
}
let currentPath = basePath;
toPath.split('/').forEach((part) => {
currentPath += `/${part}`;
if (!fs.existsSync(currentPath)) {
missingFoldersPaths.push(currentPath);
fs.mkdirSync(currentPath);
}
});
return missingFoldersPaths;
};
10 changes: 10 additions & 0 deletions packages/scaffolder-core/src/TemplatesBuilder/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ describe('TemplatesBuilder', () => {
expect(p).toBeInstanceOf(Promise);
});
});

it('should create the missing path parts', () => {
const folder = 'add/here';
(existsSync as jest.Mock).mockReturnValue(false);
const templateBuilder = new TemplatesBuilder([]);
templateBuilder.withPathPrefix(folder).build();
console.log(join(process.cwd(), folder));
expect(mkdirSync).toHaveBeenCalledWith(join(process.cwd(), 'add'));
expect(mkdirSync).toHaveBeenCalledWith(join(process.cwd(), folder));
});
});
28 changes: 26 additions & 2 deletions packages/scaffolder-core/src/TemplatesBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'fs';
import { mkdir } from 'fs-extra';
import { FolderAlreadyExists } from '../Errors';
import { join, TYPES } from '../filesUtils';
import {path as pathColor, boldGreen, bold} from '../cliHelpers/colors';
import {createMissingFoldersInPath} from './createMissingFoldersInPath';

const writeFilePromise = (path: string, content: string) =>
new Promise((resolve, reject) => {
Expand Down Expand Up @@ -42,6 +44,7 @@ export class TemplatesBuilder {
if (this.onFileWrite) {
this.onFileWrite(this.counter);
}

return data;
}

Expand All @@ -60,7 +63,12 @@ export class TemplatesBuilder {
return this;
}

createFolderIfNeeded() {
createMissingPathPartsIfNeeded() {
this.createMissingPathPrefixFolders();
this.createContainingFolder();
}

private createContainingFolder() {
if (this.folder) {
const newFolderPath = this.getFullPath();
if (fs.existsSync(newFolderPath)) {
Expand All @@ -74,6 +82,22 @@ export class TemplatesBuilder {
}
}

private createMissingPathPrefixFolders() {
const missingPaths = createMissingFoldersInPath(this.entryPoint, this.pathPrefix);
if (missingPaths.length) {
this.logMissingFoldersInPrefixPathDetected(missingPaths);
}
}

private logMissingFoldersInPrefixPathDetected(missingPaths) {
const colorEndOfPath = (path) => {
const [last, ...restOfPath] = path.split('/').reverse();
return `${restOfPath.reverse().join('/')}/${boldGreen(last)}`;
};
const missingPathsString = missingPaths.reduce((paths, path) => `${paths}\n${colorEndOfPath(path)}`, '');
console.log(bold(`Scaffolder Detected that your prefix path (${pathColor(this.pathPrefix)}) contains non existing folders and will automatically create them for you.`), missingPathsString);
}

createTemplateFolder(folderDescriptor, root) {
return mkdir(join(root, folderDescriptor.name)).then(() => {
return Promise.all(
Expand All @@ -100,7 +124,7 @@ export class TemplatesBuilder {
}

build(): Promise<any>[] {
this.createFolderIfNeeded();
this.createMissingPathPartsIfNeeded();
const promises = [];
this.templates.forEach((template) => {
const path = join(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { createTemplateStructure } from '../index';

const expectedTemplate = [
const expectedTemplateStructure = [
{
content: '',
name: 'another.js',
Expand Down Expand Up @@ -29,10 +29,11 @@ const expectedTemplate = [
type: 'FOLDER',
},
];

describe('template reader', () => {
it('should create a tree structure matching the provided template', () => {
const {templatesStructure, filesCount} = createTemplateStructure(path.join(__dirname, '/example'));
expect(templatesStructure).toEqual(expectedTemplate);
expect(templatesStructure).toEqual(expectedTemplateStructure);
expect(filesCount).toBe(4);
});
});

0 comments on commit 3424e15

Please sign in to comment.