Skip to content

Commit

Permalink
src dir and class generation with subdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
AidasPa committed Mar 11, 2021
1 parent cfbc528 commit e9a6bbb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cli/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs').promises;
const watch = require('./actions/watch');
const cli = require('./cli');

const utils = require('../utils');
const make = require('./stubs/make');

module.exports = {
Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = {
}
},
'create:actor': async (className) => {
make('actor', [['CLASSNAME', className]], `${className}.uts`);
make('actor', [['CLASSNAME', utils.getLastPathPart(className)]], `${className}.uts`);
cli.actorCreated(`${className}.uts`);
},
};
2 changes: 1 addition & 1 deletion cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function actorCreated(name) {
console.log(`
${header('ACTOR CREATED')}
${colors.green('CLASS: ')} ${colors.white(name)}
${colors.green('CLASS: ')} ${colors.white(`src/${name}`)}
${logoFormat()}
${note()}`);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/stubs/actor.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="typings/ue.d.ts">/>
/// <reference path="__ROOTPATH__typings/ue.d.ts">/>

@UCLASS()
class __CLASSNAME__ extends Actor
Expand Down
22 changes: 14 additions & 8 deletions cli/stubs/make.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
const fs = require('fs').promises;
const colors = require('colors');
const Quote = require('inspirational-quotes');
const utils = require('../../utils');
const cli = require('../cli');

module.exports = async (stubName, argArray, filePath) => {
const rawStub = await fs.readFile(`${__dirname}/${stubName}.stub`, 'utf-8');

const finalPath = `src/${filePath}`;

let computedStub = rawStub;
argArray.forEach(([key, value]) => {
const regexp = new RegExp(`__${key}__`, 'g');
computedStub = computedStub.replace(regexp, value);
});

computedStub = computedStub.replace('__QUOTE__', Quote.getRandomQuote());
computedStub = computedStub
.replace('__QUOTE__', Quote.getRandomQuote())
.replace('__ROOTPATH__', utils.calculateRootPath(finalPath));

try {
if(/\//.test(filePath)) {
const splitPath = filePath.split('/');
// splitPath
const directory = filePath.split('/')
}
await fs.writeFile(`./${filePath}`, computedStub, 'utf-8');
const splitPath = finalPath.split('/');
splitPath.pop();

const directory = splitPath.join('/');
await utils.createDirsRecursive(directory);
await fs.writeFile(`${finalPath}`, computedStub, 'utf-8');
} catch (error) {
console.log(error);
console.log(colors.red(`Did you run ${colors.bold.white('uts init')}?`));
cli.error(`Did you run ${colors.bold.white('uts init')}?`);
}
};
18 changes: 18 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,22 @@ module.exports = {
const target = file.split('.')[0];
fs.writeFile(`${target}.js`, data, 'utf-8');
},
async createDirsRecursive(path) {
await fs.mkdir(path, { recursive: true });
},
getLastPathPart(path) {
return path.split('/').pop();
},
calculateRootPath(path) {
const matches = path.match(/\//g) || [];
const len = matches.length;

let pathString = '';

for (let index = 0; index < len; index += 1) {
pathString += '../';
}

return pathString;
},
};

0 comments on commit e9a6bbb

Please sign in to comment.