Skip to content

Commit 222ccdc

Browse files
committed
feat(init): add typescript support
1 parent d313421 commit 222ccdc

12 files changed

Lines changed: 155 additions & 109 deletions

File tree

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,3 @@ lerna-debug.log
3333

3434
# Yarn lock file
3535
yarn.lock
36-
37-
# Custom typings
38-
custom_typing/*.js

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/init/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js

packages/init/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
types
2+
tsconfig.json
3+
*.ts
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
"use strict";
2-
3-
const npmPackagesExists = require("@webpack-cli/utils/npm-packages-exists");
4-
const defaultGenerator = require("@webpack-cli/generators/init-generator");
5-
const modifyConfigHelper = require("@webpack-cli/utils/modify-config-helper");
1+
import * as defaultGenerator from "@webpack-cli/generators/init-generator";
2+
import * as modifyConfigHelper from "@webpack-cli/utils/modify-config-helper";
3+
import * as npmPackagesExists from "@webpack-cli/utils/npm-packages-exists";
64

75
/**
86
*
97
* First function to be called after running the init flag. This is a check,
108
* if we are running the init command with no arguments or if we got dependencies
119
*
12-
* @param {Array} args - array of arguments such as
10+
* @param {String[]} args - array of arguments such as
1311
* packages included when running the init command
1412
* @returns {Function} creator/npmPackagesExists - returns an installation of the package,
1513
* followed up with a yeoman instance of that if there's packages. If not, it creates a defaultGenerator
1614
*/
1715

18-
module.exports = function initializeInquirer(...args) {
19-
const packages = args.slice(3);
16+
export default function initializeInquirer(...args: string[]): Function {
17+
const packages: string[] = args.slice(3);
2018

2119
if (packages.length === 0) {
2220
return modifyConfigHelper("init", defaultGenerator);
2321
}
2422
return npmPackagesExists(packages);
25-
};
23+
}

packages/init/init.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

packages/init/init.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import chalk from "chalk";
2+
import * as j from "jscodeshift";
3+
import pEachSeries = require("p-each-series");
4+
import * as path from "path";
5+
6+
import * as propTypes from "@webpack-cli/utils/prop-types";
7+
import * as astTransform from "@webpack-cli/utils/recursive-parser";
8+
import * as runPrettier from "@webpack-cli/utils/run-prettier";
9+
10+
import { IError } from "./types";
11+
import { IConfiguration, IWebpackProperties } from "./types/Transform";
12+
13+
/**
14+
*
15+
* Maps back transforms that needs to be run using the configuration
16+
* provided.
17+
*
18+
* @param {Object} transformObject - An Object with all transformations
19+
* @param {Object} config - Configuration to transform
20+
* @returns {Array} - An array with the transformations to be run
21+
*/
22+
23+
const mapOptionsToTransform = (config: IConfiguration): string[] =>
24+
Object.keys(config.webpackOptions)
25+
.filter((key: string): boolean => propTypes.has(key));
26+
27+
/**
28+
*
29+
* Runs the transformations from an object we get from yeoman
30+
*
31+
* @param {Object} webpackProperties - Configuration to transform
32+
* @param {String} action - Action to be done on the given ast
33+
* @returns {Promise} - A promise that writes each transform, runs prettier
34+
* and writes the file
35+
*/
36+
37+
export default function runTransform(webpackProperties: IWebpackProperties, action: string): void {
38+
// webpackOptions.name sent to nameTransform if match
39+
const webpackConfig: string[] =
40+
Object
41+
.keys(webpackProperties)
42+
.filter((p: string): boolean => p !== "configFile" && p !== "configPath");
43+
44+
const initActionNotDefined: boolean = (action && action !== "init") || false;
45+
46+
webpackConfig.forEach((scaffoldPiece: string): Promise<void> => {
47+
const config: IConfiguration = webpackProperties[scaffoldPiece];
48+
const transformations: string[] = mapOptionsToTransform(config);
49+
const ast = j(
50+
initActionNotDefined
51+
? webpackProperties.configFile
52+
: "module.exports = {}",
53+
);
54+
const transformAction: string | null = action || null;
55+
56+
return pEachSeries(transformations, (f: string): Promise<string[]> => {
57+
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
58+
})
59+
.then((_?: any) => {
60+
let configurationName: string = "webpack.config.js";
61+
if (config.configName) {
62+
configurationName = "webpack." + config.configName + ".js";
63+
}
64+
65+
const outputPath: string = initActionNotDefined
66+
? webpackProperties.configPath
67+
: path.join(process.cwd(), configurationName);
68+
69+
const source: string = ast.toSource({
70+
quote: "single",
71+
});
72+
73+
runPrettier(outputPath, source);
74+
})
75+
.catch((err: IError) => {
76+
console.error(err.message ? err.message : err);
77+
});
78+
});
79+
80+
if (initActionNotDefined && webpackProperties.config.item) {
81+
process.stdout.write(
82+
"\n" +
83+
chalk.green(
84+
`Congratulations! ${
85+
webpackProperties.config.item
86+
} has been ${action}ed!\n`,
87+
),
88+
);
89+
} else {
90+
process.stdout.write(
91+
"\n" +
92+
chalk.green(
93+
"Congratulations! Your new webpack configuration file has been created!\n",
94+
),
95+
);
96+
}
97+
}

packages/init/package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/init/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
"chalk": "^2.4.1",
1515
"jscodeshift": "^0.5.0",
1616
"p-each-series": "^1.0.0"
17+
},
18+
"devDependencies": {
19+
"@types/node": "^10.3.6",
20+
"@types/p-each-series": "^1.0.0",
21+
"typescript": "^2.9.2"
22+
},
23+
"scripts": {
24+
"build": "tsc"
1725
}
1826
}

packages/init/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.json"
3+
}

0 commit comments

Comments
 (0)