Skip to content

Commit 9010ecb

Browse files
committed
Add create command
1 parent a246365 commit 9010ecb

8 files changed

Lines changed: 115 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ TSDX comes with the "battery-pack included" and is part of a complete TypeScript
2020
## Quick Start
2121

2222
```
23-
npx tsdx create my-lib
23+
yarn tsdx create my-lib
2424
cd my-lib
25-
npm start
25+
yarn start
2626
```
2727

2828
That's it. You don't need to worry about setting up Typescript or Rollup or Jest or other plumbing. Just start editing `src/index.ts` and go!

lib/index.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const jest = require('jest');
1313
const json = require('rollup-plugin-json');
1414
const logError = require('./logError');
1515
const path = require('path');
16-
const prog = sade('tsdx');
16+
const mkdirp = require('mkdirp');
1717
const replace = require('rollup-plugin-replace');
1818
const resolve = require('rollup-plugin-node-resolve');
1919
const sourceMaps = require('rollup-plugin-sourcemaps');
2020
const typescript = require('rollup-plugin-typescript2');
21+
const execa = require('execa');
2122

23+
const prog = sade('tsdx');
2224
// Make sure any symlinks in the project folder are resolved:
2325
// https://github.com/facebookincubator/create-react-app/issues/637
2426
const appDirectory = fs.realpathSync(process.cwd());
@@ -226,6 +228,62 @@ prog
226228
}
227229
});
228230

231+
prog
232+
.command('create <pkg>')
233+
.describe('Create a new package with TSDX')
234+
.action(async pkg => {
235+
try {
236+
console.log('Bootstrapping new project...');
237+
const projectPath = process.cwd() + '/' + pkg;
238+
// copy the template
239+
await fs.copy(path.resolve(__dirname, './template'), projectPath, {
240+
overwrite: true,
241+
});
242+
// fix gitignore
243+
await fs.move(
244+
path.resolve(projectPath, './gitignore'),
245+
path.resolve(projectPath, './.gitignore')
246+
);
247+
// Install deps
248+
process.chdir(projectPath);
249+
const safeName = safeVariableName(pkg);
250+
const pkgJson = {
251+
name: safeName,
252+
version: '0.1.0',
253+
source: 'src/index.ts',
254+
main: 'index.js',
255+
'umd:main': `dist/${safeName}.umd.production.js`,
256+
module: `dist/${safeName}.es.production.js`,
257+
typings: 'dist/index.d.ts',
258+
scripts: {
259+
start: 'tsdx watch',
260+
build: 'tsdx build',
261+
test: 'tsdx test',
262+
},
263+
};
264+
await fs.outputJSON(path.resolve(projectPath, 'package.json'), pkgJson);
265+
266+
console.log('Installing dependencies...');
267+
268+
await execa(`yarn`, [
269+
'add',
270+
'@types/jest',
271+
'tsdx',
272+
'typescript',
273+
'--dev',
274+
]);
275+
console.log(`
276+
Success!! TSDX just bootstrapped a brand new project for you. To get started, run:
277+
278+
cd ${pkg}
279+
yarn start
280+
281+
`);
282+
} catch (error) {
283+
logError(error);
284+
}
285+
});
286+
229287
prog
230288
.command('test')
231289
.describe(

lib/template/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# TSDX Bootstrap
2+
3+
This project was bootstrapped with [TSDX](https://github.com/jaredpalmer/tsdx).

lib/template/gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
.rts2_cache_cjs
5+
.rts2_cache_es
6+
.rts2_cache_umd
7+
dist

lib/template/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const sum = (a: number, b: number) => {
2+
if ('development' === process.env.NODE_ENV) {
3+
console.log('fuck');
4+
}
5+
return a + b;
6+
};

lib/template/test/blah.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { sum } from '../src';
2+
3+
describe('fuck', () => {
4+
it('works', () => {
5+
expect(sum(1, 1)).toEqual(2);
6+
});
7+
});

lib/template/tsconfig.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "ESNext",
5+
"lib": ["dom", "esnext"],
6+
"declaration": true,
7+
"sourceMap": true,
8+
"rootDir": "./",
9+
"strict": true,
10+
"noImplicitAny": true,
11+
"strictNullChecks": true,
12+
"strictFunctionTypes": true,
13+
"strictPropertyInitialization": true,
14+
"noImplicitThis": true,
15+
"alwaysStrict": true,
16+
"noUnusedLocals": true,
17+
"noUnusedParameters": true,
18+
"noImplicitReturns": true,
19+
"noFallthroughCasesInSwitch": true,
20+
"moduleResolution": "node",
21+
"baseUrl": "./",
22+
"paths": {
23+
"*": ["src/*", "node_modules/*"]
24+
},
25+
"jsx": "react",
26+
"esModuleInterop": true
27+
},
28+
"include": ["src", "types"],
29+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
"camelcase": "^5.0.0",
1818
"chalk": "^2.4.2",
1919
"cross-env": "5.0.5",
20+
"execa": "^1.0.0",
2021
"fs-extra": "^7.0.1",
2122
"jest": "23.6.0",
23+
"mkdirp": "^0.5.1",
2224
"rollup": "^0.66.4",
2325
"rollup-plugin-babel": "^4.0.3",
2426
"rollup-plugin-commonjs": "^9.1.8",

0 commit comments

Comments
 (0)