Skip to content

Commit

Permalink
bug fixes on template example and added new pattern 'path()'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaumesegarra committed Apr 8, 2019
1 parent da666d3 commit d002b18
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 15 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ Steps:
- "component": It's the name of the folder inside `_templates_`.
- "helloComponent": Path where the new thing will generated and also: the name of thing.

## Patterns
Example command: `npm run g component src/hello`

- **%name%** : Stuff name: if you run it, you'll get ‘hello’.
- **%Name%** : Stuff name capitalized: if you run it, you'll get ‘Hello’.
- **%path('some_path/file.js')%**: get relative path for the new stuff location ('src/hello/').

If you run it, you'll get '../../some_path/file.js'.

Please, put the file path on template **relative to your package.json**.

## License

[MIT](https://github.com/jaumesegarra/xtuff/blob/master/LICENSE)
6 changes: 5 additions & 1 deletion _templates_/component/%name%.jsx.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import React from 'react';

import './<%=name%>.scss';

const <%=Name%>Component = ({ }) => {
const pkg = require('<%=path('package.json')%>');

const <%=Name%>Component = () => {
console.info(`App version: ${pkg.version}`);

return (
<div className="<%=name%>">
<%=Name%> works fine!
Expand Down
2 changes: 1 addition & 1 deletion _templates_/component/%name%.scss.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.<%=name%>{

position: relative;
}
3 changes: 2 additions & 1 deletion _templates_/component/%name%.test.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import ReactDOM from 'react-dom';
import <%=Name%>Component from './<%=name%>.jsx';

describe('<<%=Name%>Component />', () => {

let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
Expand Down
16 changes: 12 additions & 4 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs-extra');
const tmp = require('tmp');
const ejs = require('ejs');
const utils = require('./utils.js');
const relative = require('relative');

const EJS_EXTENSION = '.ejs';

Expand Down Expand Up @@ -33,11 +34,17 @@ const generateFileFromTemplate = (stuffName, resourcePath, destinyFolderPath) =>

const component = ejs.render(data, {
name: stuffName,
Name: stuffName.toCapitalize()
Name: stuffName.toCapitalize(),
path: (v) => {
const filePath = path.join(utils.getPackageFolder(), v);

return relative(absoluteStuffPath, filePath);
}
});

const filename = path.basename(resourcePath).toLowerCase()
const filename = path.basename(resourcePath)
.replace('%name%', stuffName)
.replace('%Name%', stuffName.toCapitalize())
.replace(EJS_EXTENSION, '');

fs.writeFile(path.join(destinyFolderPath, filename), component);
Expand Down Expand Up @@ -94,17 +101,18 @@ const moveToPackageDestiny = (folderPath, destinyFolderPath) => new Promise((res
});
});

let absoluteStuffPath;
module.exports = (generatorName, stuffPath) => {
const generatorTemplateFolder = path.join(utils.getTemplatesFolder(), generatorName);

if (fs.existsSync(generatorTemplateFolder)) {

const stuffName = getStuffName(stuffPath);
if(!path.isAbsolute(stuffPath)) stuffPath = path.join(process.env.INIT_CWD, stuffPath);
absoluteStuffPath = !path.isAbsolute(stuffPath) ? path.join(process.env.INIT_CWD, stuffPath) : stuffPath;

createTempFolder().then(cacheFolderPath => {
copyResourcesToTempFolder(stuffName, generatorTemplateFolder, cacheFolderPath).then(() => {
moveToPackageDestiny(cacheFolderPath, stuffPath);
moveToPackageDestiny(cacheFolderPath, absoluteStuffPath);
});
});
}
Expand Down
123 changes: 116 additions & 7 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xtuff",
"version": "1.0.6",
"version": "1.1.0",
"description": "A dev command to create stuffs easy! (components, services, etc...)",
"keywords": "command, generator, component, service, react, stuff, files",
"repository": "https://github.com/jaumesegarra/xtuff",
Expand All @@ -19,6 +19,7 @@
"ejs": "^2.6.1",
"fs-extra": "^7.0.1",
"path": "^0.12.7",
"relative": "^3.0.2",
"tmp": "^0.1.0"
}
}
1 change: 1 addition & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const getPackageFolder = () => path.join(__dirname, '../../');
const getTemplatesFolder = () => path.join(getPackageFolder(), TEMPLATE_FOLDER_NAME);

module.exports = {
getPackageFolder,
getTemplatesFolder,
TEMPLATE_FOLDER_NAME
}

0 comments on commit d002b18

Please sign in to comment.