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 Jul 6, 2018
1 parent d313421 commit 222ccdc
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 109 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ lerna-debug.log

# Yarn lock file
yarn.lock

# Custom typings
custom_typing/*.js
6 changes: 3 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
3 changes: 3 additions & 0 deletions packages/init/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
types
tsconfig.json
*.ts
16 changes: 7 additions & 9 deletions packages/init/index.js → packages/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
"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 * as defaultGenerator from "@webpack-cli/generators/init-generator";
import * as modifyConfigHelper from "@webpack-cli/utils/modify-config-helper";
import * as npmPackagesExists from "@webpack-cli/utils/npm-packages-exists";

/**
*
* 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[]): Function {
const packages: string[] = args.slice(3);

if (packages.length === 0) {
return modifyConfigHelper("init", defaultGenerator);
}
return npmPackagesExists(packages);
};
}
94 changes: 0 additions & 94 deletions packages/init/init.js

This file was deleted.

97 changes: 97 additions & 0 deletions packages/init/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import chalk from "chalk";
import * as j from "jscodeshift";
import pEachSeries = require("p-each-series");
import * as path from "path";

import * as propTypes from "@webpack-cli/utils/prop-types";
import * as astTransform from "@webpack-cli/utils/recursive-parser";
import * as runPrettier from "@webpack-cli/utils/run-prettier";

import { IError } from "./types";
import { IConfiguration, IWebpackProperties } from "./types/Transform";

/**
*
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Array} - An array with the transformations to be run
*/

const mapOptionsToTransform = (config: IConfiguration): string[] =>
Object.keys(config.webpackOptions)
.filter((key: string): boolean => propTypes.has(key));

/**
*
* Runs the transformations from an object we get from yeoman
*
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
*/

export default function runTransform(webpackProperties: IWebpackProperties, action: string): void {
// webpackOptions.name sent to nameTransform if match
const webpackConfig: string[] =
Object
.keys(webpackProperties)
.filter((p: string): boolean => p !== "configFile" && p !== "configPath");

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

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

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

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

const source: string = ast.toSource({
quote: "single",
});

runPrettier(outputPath, source);
})
.catch((err: IError) => {
console.error(err.message ? err.message : err);
});
});

if (initActionNotDefined && webpackProperties.config.item) {
process.stdout.write(
"\n" +
chalk.green(
`Congratulations! ${
webpackProperties.config.item
} has been ${action}ed!\n`,
),
);
} else {
process.stdout.write(
"\n" +
chalk.green(
"Congratulations! Your new webpack configuration file has been created!\n",
),
);
}
}
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.

8 changes: 8 additions & 0 deletions packages/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
"chalk": "^2.4.1",
"jscodeshift": "^0.5.0",
"p-each-series": "^1.0.0"
},
"devDependencies": {
"@types/node": "^10.3.6",
"@types/p-each-series": "^1.0.0",
"typescript": "^2.9.2"
},
"scripts": {
"build": "tsc"
}
}
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.json"
}
15 changes: 15 additions & 0 deletions packages/init/types/Transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface IWebpackProperties extends Object {
configFile: string;
configPath: string;
webpackOptions: IConfiguration;
config: {
item: string;
configName: string;
};
}

export interface IConfiguration extends Object {
configName: string;
webpackOptions: object;
topScope: string[];
}
3 changes: 3 additions & 0 deletions packages/init/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IError {
message?: string;
}

0 comments on commit 222ccdc

Please sign in to comment.