【问题标题】:Automated Webpack Project Setup through node通过节点自动设置 Webpack 项目
【发布时间】:2017-07-01 16:16:56
【问题描述】:

我的目标是通过节点命令 crateEntireWepbackProject.js 文件来设置一个 webpack 项目。

我想从 js 文件在 shell 上执行命令,这样我就可以让它们自动运行,然后还包括项目的自定义规范。

js 文件不是来自 webpack,但有命令从头开始创建 wepback 项目并进行安装等,只需我输入node createEntireWebpackProject.js

【问题讨论】:

  • 你需要 package.json 文件。
  • 我的意思是一个外部 index.js 文件,它通过节点为我设置 package.json 和所有其他东西

标签: javascript node.js terminal


【解决方案1】:

您不需要从头开始编写它。最佳做法是使用yeoman。有很多带有 webpack 的生成器。例如:

const Generator = require('yeoman-generator');
const mkdirp = require('mkdirp');
const path = require('path');

module.exports = class extends Generator {
  prompting() {
    this.log('Welcome to the classy example generator!');

    const prompts = [
      {
        type: 'input',
        name: 'name',
        message: 'Name?',
        default: this.appname,
      }];

    return this.prompt(prompts).then((props) => {
      this.props = props;
    });
  }

  default() {
    if (path.basename(this.destinationPath()) !== this.props.name) {
      this.log(
        `Your application must be inside a folder named ${this.props.name}`);
      this.log('I\'ll automatically create this folder.');
      mkdirp(this.props.name);
      this.destinationRoot(this.destinationPath(this.props.name));
    }
  }

  writing() {
    this.createPackageJson();
    this.copyFiles();
    this.fillTemplates();
    this.makeCommands();
  }

  install() {
    this.npmInstall(['bunyan', 'dotenv-safe'], { save: true });
    this.npmInstall(['eslint', 'eslint-config-airbnb-base', 'eslint-plugin-import', 'jest'], { 'save-dev': true });
  }

  createPackageJson() {
    this.fs.extendJSON('package.json', {
      name: this.props.name,
      version: '0.1.0',
      main: 'src/app.js',
      scripts: {
        cs: 'eslint src/* __tests__/*',
        'cs:fix': 'eslint src/* __tests__/* --fix',
        start: 'node src/app.js',
        test: 'npm run cs && jest',
      },
      dependencies: {},
      devDependencies: {},
      engines: {
        node: '^8.1.0',
      },
      private: true,
      jest: {
        testEnvironment: 'node',
        transform: {},
        collectCoverage: true,
      },
    });
  }

  copyFiles() {
    [
      '.dockerignore',
      '.eslintrc.json',
      'src/app.js',
    ].forEach(name => this.fs.copy(this.templatePath(name), this.destinationPath(name)));
  }

  fillTemplates() {
    this.fs.copyTpl(
      this.templatePath('README.md'),
      this.destinationPath('README.md'),
      {
        name: this.props.name,
      });
  }
  makeCommands() {
      this.spawnCommandSync('git' ['init']);
      this.spawnCommandSync('git', ['add', '.']);
      this.spawnCommandSync('git', ['commit', '-am', '"yo scaffolded app"']);
  }
};

【讨论】:

  • 设置项目可能不需要命令,所以我想知道如何自己设置它,因为我稍后想从 photoshop 读取文件并创建文件和使用 webpack 自动设置它们,所以我需要创建自己的代码。但是我需要在js中写入什么才能使其成为命令行代码?
  • Fabioo Fabiolous,我用一个例子更新了我的答案。在makeCommands 函数中,您可以找到如何在生成器中运行命令行代码。
  • 我明白了,这很棒。你认为我的整个流程都会被这个系统覆盖吗?
  • yeoman 可以自动完成您在终端中可以做的任何事情。
猜你喜欢
  • 2017-11-13
  • 2013-12-15
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多