UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.name = exports.description = void 0;
7exports.run = run;
8exports.setup = setup;
9
10var _path = _interopRequireDefault(require("path"));
11
12var _fs = _interopRequireDefault(require("fs"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16const flowVersion = '0.83.x';
17const name = 'create-def <libName> <ver>';
18exports.name = name;
19const description = 'Create template library definitions in flow-typed';
20exports.description = description;
21
22function setup(yargs) {
23 return yargs.usage(`$0 ${name}`).positional('libName', {
24 describe: 'Please provide the names of library',
25 type: 'string'
26 }).positional('ver', {
27 describe: 'Please provide the version range you want to add',
28 type: 'string'
29 }).example('$0 create-def foo 1.x.x', '').help('h').alias('h', 'help');
30}
31
32async function run({
33 libName,
34 ver
35}) {
36 if (typeof libName !== 'string' || typeof ver !== 'string') {
37 return 1;
38 }
39
40 if (ver.startsWith('v')) {
41 console.error("ERROR: You don't need to specify `v` with your version");
42 return 1;
43 }
44
45 const scope = libName.startsWith('@') ? libName.split('/')[0] : '';
46 const packageName = scope ? libName.split('/')[1] : libName;
47
48 const definitionsPath = _path.default.join(process.cwd(), '/definitions/npm', scope !== null && scope !== void 0 ? scope : '');
49
50 const rootDefDir = `${definitionsPath}/${packageName}_v${ver}`;
51 const defDir = `${rootDefDir}/flow_v${flowVersion}-`;
52
53 if (scope) {
54 try {
55 _fs.default.mkdirSync(definitionsPath);
56 } catch (err) {
57 if (err.code !== 'EEXIST') {
58 throw err;
59 }
60 }
61 }
62
63 _fs.default.mkdirSync(rootDefDir);
64
65 _fs.default.mkdirSync(defDir);
66
67 _fs.default.writeFileSync(`${defDir}/${packageName}_v${ver}.js`, `declare module '${libName}' {
68 declare module.exports: any;
69}`);
70
71 _fs.default.writeFileSync(`${defDir}/test_${packageName}_v${ver}.js`, `// @flow
72import { describe, it } from 'flow-typed-test';
73// import library from '${libName}';
74
75describe('${libName}', () => {
76 it('', () => {
77
78 });
79});`);
80
81 return 0;
82}
\No newline at end of file