Skip to content

Commit

Permalink
feat(init): add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Jun 19, 2018
1 parent ba67cf1 commit 3ca5b37
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
2 changes: 2 additions & 0 deletions packages/init/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tsconfig.json
*.ts
12 changes: 6 additions & 6 deletions packages/init/index.js → packages/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"use strict";

const npmPackagesExists = require("@webpack-cli/utils/npm-packages-exists");
const defaultGenerator = require("@webpack-cli/generators/init-generator");
const modifyConfigHelper = require("@webpack-cli/utils/modify-config-helper");
import npmPackagesExists from "@webpack-cli/utils/npm-packages-exists";
import defaultGenerator from "@webpack-cli/generators/init-generator";
import modifyConfigHelper from "@webpack-cli/utils/modify-config-helper";

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

module.exports = function initializeInquirer(...args) {
const packages = args.slice(3);
export default function initializeInquirer(...args: string[]) {
const packages: string[] = args.slice(3);

if (packages.length === 0) {
return modifyConfigHelper("init", defaultGenerator);
Expand Down
52 changes: 27 additions & 25 deletions packages/init/init.js → packages/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

const path = require("path");
const j = require("jscodeshift");
const chalk = require("chalk");
const pEachSeries = require("p-each-series");
import path from "path";
import j from "jscodeshift";
import chalk from "chalk";
import pEachSeries from "p-each-series";

const runPrettier = require("@webpack-cli/utils/run-prettier");
const astTransform = require("@webpack-cli/utils/recursive-parser");
const propTypes = require("@webpack-cli/utils/prop-types");
import runPrettier from "@webpack-cli/utils/run-prettier";
import astTransform from "@webpack-cli/utils/recursive-parser";
import propTypes from "@webpack-cli/utils/prop-types";

/**
*
Expand All @@ -16,12 +16,12 @@ const propTypes = require("@webpack-cli/utils/prop-types");
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Object} - An Object with the transformations to be run
* @returns {Array} - An array with the transformations to be run
*/

function mapOptionsToTransform(config) {
return Object.keys(config.webpackOptions).filter(k => propTypes.has(k));
}
const mapOptionsToTransform = (config): string[] =>
Object.keys(config.webpackOptions)
.filter((key: string): boolean => propTypes.has(key))

/**
*
Expand All @@ -33,37 +33,38 @@ function mapOptionsToTransform(config) {
* and writes the file
*/

module.exports = function runTransform(webpackProperties, action) {
export default function runTransform(webpackProperties, action: string) {
// webpackOptions.name sent to nameTransform if match
const webpackConfig = Object.keys(webpackProperties).filter(p => {
return p !== "configFile" && p !== "configPath";
});
const initActionNotDefined = action && action !== "init" ? true : false;
const webpackConfig: string[] =
Object
.keys(webpackProperties)
.filter((p: string): boolean => p !== "configFile" && p !== "configPath");

const initActionNotDefined: boolean = (action && action !== "init") || false;

webpackConfig.forEach(scaffoldPiece => {
webpackConfig.forEach((scaffoldPiece: string): Promise<void> => {
const config = webpackProperties[scaffoldPiece];
const transformations = mapOptionsToTransform(config);
const transformations: string[] = mapOptionsToTransform(config);
const ast = j(
initActionNotDefined
? webpackProperties.configFile
: "module.exports = {}"
);
const transformAction = action || null;
const transformAction: string | null = action || null;

return pEachSeries(transformations, f => {
return pEachSeries(transformations, (f: string) => {
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
})
.then(_ => {
let configurationName;
if (!config.configName) {
configurationName = "webpack.config.js";
} else {
let configurationName: string = "webpack.config.js";
if (config.configName) {
configurationName = "webpack." + config.configName + ".js";
}

const outputPath = initActionNotDefined
const outputPath: string = initActionNotDefined
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);

const source = ast.toSource({
quote: "single"
});
Expand All @@ -74,6 +75,7 @@ module.exports = function runTransform(webpackProperties, action) {
console.error(err.message ? err.message : err);
});
});

if (initActionNotDefined && webpackProperties.config.item) {
process.stdout.write(
"\n" +
Expand Down
15 changes: 15 additions & 0 deletions packages/init/package-lock.json

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

9 changes: 9 additions & 0 deletions packages/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@
"chalk": "^2.4.1",
"jscodeshift": "^0.5.0",
"p-each-series": "^1.0.0"
},
"devDependencies": {
"@types/node": "^10.3.3",
"@types/p-each-series": "^1.0.0",
"typescript": "^2.9.2"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w"
}
}
3 changes: 3 additions & 0 deletions packages/init/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json",
}

0 comments on commit 3ca5b37

Please sign in to comment.