-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
155 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,3 @@ lerna-debug.log | |
|
||
# Yarn lock file | ||
yarn.lock | ||
|
||
# Custom typings | ||
custom_typing/*.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
types | ||
tsconfig.json | ||
*.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IError { | ||
message?: string; | ||
} |