Skip to content

Commit

Permalink
feat: almost finalize serve cli
Browse files Browse the repository at this point in the history
  • Loading branch information
swashata committed Oct 9, 2018
1 parent b4f5fab commit 58a9179
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 249 deletions.
2 changes: 1 addition & 1 deletion examples/plugin/src/app/mobile.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Hello Mobile');
console.log('Hello Mobile!');
2 changes: 1 addition & 1 deletion examples/plugin/wpackio.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
// Whether to show the "BrowserSync Connected"
notify: false,
// Open the dev server URL, set false to disable
open: true,
open: false,
// BrowserSync ghostMode, set to false to completely disable
ghostMode: {
clicks: true,
Expand Down
29 changes: 29 additions & 0 deletions packages/scripts/@types/boxen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
declare module 'boxen' {
type borderstyle = 'single' | 'double' | 'round' | 'single-double'
| 'double-single' | 'classic' | {
topLeft: string,
topRight: string,
bottomLeft: string,
bottomRight: string,
horizontal: string,
vertical: string,
}
interface CssProperty {
top: number;
right: number;
bottom: number;
left: number;
}
type alignment = 'right' | 'center' | 'left';
interface Options {
borderColor?: string;
borderStyle?: borderstyle;
dimBorder?: boolean;
padding?:number | CssProperty;
margin?: number | CssProperty;
float?: alignment;
backgroundColor?: string;
align?: alignment;
}
export default function boxen(input: string, options: Options): string;
}
3 changes: 3 additions & 0 deletions packages/scripts/@types/react-dev-utils/clearConsole.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'react-dev-utils/clearConsole' {
export default function clearConsole():void;
}
3 changes: 3 additions & 0 deletions packages/scripts/@types/terminal-link.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'terminal-link' {
export default function terminalLink(txt:string, link:string):string;
}
17 changes: 15 additions & 2 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,40 @@
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-typescript": "^7.1.0",
"@types/browser-sync": "^0.0.42",
"@types/figlet": "^1.2.0",
"@types/figures": "^2.0.0",
"@types/find-up": "^2.1.1",
"@types/gradient-string": "^1.1.0",
"@types/log-symbols": "^2.0.0",
"@types/node": "^10.11.3",
"@types/signale": "^1.2.0",
"@types/ora": "^1.3.4",
"@types/webpack": "^4.4.13",
"@types/webpack-assets-manifest": "^3.0.0",
"@types/webpack-dev-middleware": "^2.0.2",
"@types/webpack-hot-middleware": "^2.16.4",
"@wpackio/babel-preset-base": "0.0.1",
"autoprefixer": "^9.1.5",
"babel-loader": "^8.0.2",
"boxen": "^2.0.0",
"browser-sync": "^2.24.7",
"chalk": "^2.4.1",
"clean-webpack-plugin": "^0.1.19",
"commander": "^2.18.0",
"css-loader": "^1.0.0",
"dev-ip": "^1.0.1",
"figlet": "^1.2.0",
"figures": "^2.0.0",
"file-loader": "^2.0.0",
"find-up": "^3.0.0",
"gradient-string": "^1.2.0",
"log-symbols": "^2.2.0",
"mini-css-extract-plugin": "^0.4.3",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"ora": "^3.0.0",
"postcss-loader": "^3.0.0",
"pretty-error": "^2.1.1",
"react-dev-utils": "^6.0.4",
"sass-loader": "^7.1.0",
"signale": "^1.3.0",
"slugify": "^1.3.1",
"style-loader": "^0.23.0",
"uglifyjs-webpack-plugin": "^2.0.1",
Expand Down
52 changes: 52 additions & 0 deletions packages/scripts/src/bin/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Build } from '../scripts/Build';
import { getProjectAndServerConfig } from './getProjectAndServerConfig';
import { ProgramOptions } from './index';
import { resolveCWD } from './utils';

/**
* Start the `wpackio-scripts build` command.
*
* @param options Option as received from CLI.
*/
export function build(options: ProgramOptions | undefined): void {
console.log('Creating production builds...');
// Set process.env.NODE_ENV to production
process.env.NODE_ENV = 'production';
// Set process.env.BABEL_ENV to production
process.env.BABEL_ENV = 'production';
// Get project and server config JSONs.
const cwd = resolveCWD(options);
console.log(`Using startup path: ${cwd}`);
try {
const {
projectConfig,
serverConfig,
projectConfigPath,
serverConfigPath,
} = getProjectAndServerConfig(cwd, options);
console.log(`Using project config from ${projectConfigPath}`);
console.log(`Using server config from ${serverConfigPath}`);
// Start the webpack/browserSync server
const builder: Build = new Build(projectConfig, serverConfig, cwd);
builder
.build()
.then(log => {
console.log('Build Successful. Please check the log below');
console.log(log);
process.exit(0);
})
.catch(err => {
console.error(
'Could not create production build. Please check the log below'
);
console.log(err);
process.exit(1);
});
} catch (e) {
console.error(
'Could not start development server. Please check the log below.'
);
console.error(e);
process.exit(1);
}
}
68 changes: 68 additions & 0 deletions packages/scripts/src/bin/getProjectAndServerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import path from 'path';
import { ProjectConfig } from '../config/project.config.default';
import { ServerConfig } from '../config/server.config.default';
// tslint:disable: non-literal-require
export function getProjectAndServerConfig(
cwd: string,
options:
| {
projectConfig?: string;
serverConfig?: string;
}
| undefined
): {
projectConfig: ProjectConfig;
serverConfig: ServerConfig;
projectConfigPath: string;
serverConfigPath: string;
} {
// Get the config file paths from options
// If user is passing relative path, then it will be used along with cwd
// If it is absolute path, then the absolute would be used instead
// This is how path.resolve works.
const projectConfigPath = path.resolve(
cwd,
options && options.projectConfig
? options.projectConfig
: 'wpackio.project.js'
);
const serverConfigPath = path.resolve(
cwd,
options && options.serverConfig
? options.serverConfig
: 'wpackio.server.js'
);
// Now create the configuration objects
let projectConfig: ProjectConfig;
let serverConfig: ServerConfig;
// First check to see if the files are present
try {
projectConfig = require(projectConfigPath) as ProjectConfig;
} catch (e) {
throw new Error(
`Could not find project configuration at:\n${projectConfigPath}\nPlease make sure the file exists or adjust your --context or --project-config parameters.`
);
}
try {
serverConfig = require(serverConfigPath) as ServerConfig;
} catch (e) {
throw new Error(
`Could not find server configuration at:\n${serverConfigPath}\nPlease make sure the file exists or adjust your --context or --server-config parameters.`
);
}
// Now validate them
if (typeof projectConfig !== 'object') {
throw new Error(
`Project configuration must export an object literal. Right now it is ${typeof projectConfig}`
);
}
if (typeof serverConfig !== 'object') {
throw new Error(
`Server configuration must export an object literal. Right now it is ${typeof serverConfig}`
);
}
// @todo
// Also validate the config, but let's leave it for now
// Make sure to do it in future
return { projectConfig, serverConfig, projectConfigPath, serverConfigPath };
}
Loading

0 comments on commit 58a9179

Please sign in to comment.