Skip to content

Commit 3e074f7

Browse files
author
k.golikov
committed
Add app scripts
1 parent 240ce7a commit 3e074f7

File tree

14 files changed

+1035
-1
lines changed

14 files changed

+1035
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
/_scripts/dist/

_scripts/commands/createPage.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { appRootPath } from '../common';
4+
import { camelCase } from 'lodash';
5+
import pascalCase from '../utils/pascalCase';
6+
import { exec } from 'child_process';
7+
8+
const pagesDirectoryPath = path.resolve(appRootPath, 'src/pages');
9+
10+
const getTsxContent = (pageName: string): string => {
11+
return `
12+
import React, { FunctionComponent } from 'react';
13+
import PageContainer from '../../components/pageContainer/PageContainer';
14+
import styles from './${pageName}.module.scss';
15+
16+
const ${pageName}: FunctionComponent = () => {
17+
return (
18+
<PageContainer title="${pageName}">
19+
20+
</PageContainer>
21+
);
22+
};
23+
24+
export default ${pageName};
25+
`.trim();
26+
};
27+
28+
interface Args {
29+
force?: boolean;
30+
}
31+
32+
const createPage = async (name: string, args: Args) => {
33+
const { force } = args;
34+
35+
const pageDirectory = path.resolve(pagesDirectoryPath, camelCase(name));
36+
37+
if (fs.existsSync(pageDirectory)) {
38+
if (!force) {
39+
console.log('Directory already exists. Aborted');
40+
return;
41+
}
42+
} else {
43+
await fs.promises.mkdir(pageDirectory);
44+
console.log('Created directory ' + pageDirectory);
45+
}
46+
47+
const componentName = pascalCase(name);
48+
49+
const componentFilePath = path.resolve(pageDirectory, `${componentName}.tsx`);
50+
const stylesFilePath = path.resolve(pageDirectory, `${componentName}.module.scss`);
51+
52+
await fs.promises.writeFile(componentFilePath, getTsxContent(componentName), { encoding: 'utf-8' });
53+
console.log('Created file ' + componentFilePath);
54+
55+
await fs.promises.writeFile(stylesFilePath, '', { encoding: 'utf-8' });
56+
console.log('Created file ' + stylesFilePath);
57+
58+
exec(`git add "${componentFilePath}"`, (error, stdout, stderr) => {
59+
console.log(error || stderr || stdout);
60+
61+
exec(`git add "${stylesFilePath}"`, (error, stdout, stderr) => {
62+
console.log(error || stderr || stdout);
63+
});
64+
});
65+
};
66+
67+
export default createPage;

_scripts/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as path from 'path';
2+
3+
export const appRootPath = path.resolve(__dirname.replace(/\\dist$/, ''), '..');

_scripts/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Command } from 'commander';
2+
import createPage from './commands/createPage';
3+
4+
const program = new Command();
5+
6+
program
7+
.command('create-page')
8+
.description('Create a page')
9+
.argument('<string>', 'new page name or path')
10+
.option('--force', 'Overwrite existing page')
11+
.action(async (name, args) => createPage(name, args));
12+
13+
program.parse();

_scripts/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "mrgrd56.github.io-scripts",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"app-scripts": "node ./dist/index.js",
6+
"app-scripts:ts": "ts-node ./index.ts",
7+
"app-scripts:build": "tsc"
8+
},
9+
"dependencies": {
10+
"lodash": "^4.17.21",
11+
"commander": "^9.2.0"
12+
},
13+
"devDependencies": {
14+
"nodemon": "^2.0.16",
15+
"ts-node": "^10.7.0",
16+
"typescript": "^4.6.4",
17+
"@types/lodash": "^4.14.182",
18+
"@types/node": "^17.0.31"
19+
}
20+
}

_scripts/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"sourceMap": true,
6+
"rootDir": ".",
7+
"outDir": "./dist"
8+
},
9+
"include": ["./"],
10+
"exclude": ["node_modules", "./dist"]
11+
}

_scripts/utils/capitalizeFirst.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const capitalizeFirst = (value: string): string => {
2+
return value.charAt(0).toUpperCase() + value.slice(1);
3+
};
4+
5+
export default capitalizeFirst;

_scripts/utils/pascalCase.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { camelCase } from 'lodash';
2+
import capitalizeFirst from './capitalizeFirst';
3+
4+
const pascalCase = (string: string) => {
5+
return capitalizeFirst(camelCase(string));
6+
};
7+
8+
export default pascalCase;

0 commit comments

Comments
 (0)