Skip to content

Commit

Permalink
Linting, dependencies, and output dirs/files
Browse files Browse the repository at this point in the history
Rather than trying to cobble together a decent story for linting an Electron project inside an Ember project, when the Electron tooling doesn't even really have a linting story, let's just disable it in the Electron project. electron-forge by default doesn't configure the project to lint, so let's leave it to users to do their selves and/or for some future time when we have more real-world feedback and/or electron-forge has a linting story.

Make the blueprint also update .travis.yml to install dependencies in the Electron project along with Ember dependencies.

Stop customizing the output directory, and let it stay where electron-forge wants to put it by default. This leaves us more in line with the default tooling, which may make things easier down the road. Also, configure the Electron app's defualt package.json to tell electron-packager to ignore our test and test build directories, so packaged apps don't end up bloated with extra stuff.
  • Loading branch information
bendemboski committed Nov 24, 2019
1 parent 5bcea88 commit b820e2f
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 138 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# compiled output
/dist/
/tmp/
/electron-out/

# dependencies
/bower_components/
Expand Down
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
/bower.json
/config/ember-try.js
/dist
/electron-out
/CONTRIBUTING.md
/ember-cli-build.js
/tmp
Expand Down
1 change: 0 additions & 1 deletion blueprints/ember-electron/files/testem-electron.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-env node */
module.exports = {
test_page: 'tests/index.html?hidepassed',
disable_watching: true,
Expand Down
84 changes: 44 additions & 40 deletions blueprints/ember-electron/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
const Blueprint = require('ember-cli/lib/models/blueprint');
const { api } = require('@electron-forge/core');
const chalk = require('chalk');
const {
electronProjectPath,
emberBuildDir,
emberTestBuildDir
} = require('../../lib/utils/build-paths');
const path = require('path');
const { electronProjectPath } = require('../../lib/utils/build-paths');
const { promisify } = require('util');
const fs = require('fs');
const readFile = promisify(fs.readFile);
Expand Down Expand Up @@ -52,14 +47,15 @@ module.exports = class EmberElectronBlueprint extends Blueprint {
async afterInstall() {
await this.updateTravisYml();
await this.updateEslintIgnore();
await this.updateEslintRc();
await this.createElectronProject();
}

async updateTravisYml() {
if (!fs.existsSync('.travis.yml')) {
this.ui.writeLine(chalk.yellow([
`\nNo .travis.yml found to update. For info on manually updating your CI'`,
`'config read ${ciUrl}'\n`
`\nNo .travis.yml found to update. For info on manually updating your CI`,
`config read ${ciUrl}\n`
].join(' ')));
return;
}
Expand All @@ -70,6 +66,7 @@ module.exports = class EmberElectronBlueprint extends Blueprint {
let contents = await readFile('.travis.yml');
let yawn = new YAWN(contents.toString());

// Add xvfb to the packages
let doc = yawn.json;
doc.addons = doc.addons || {};
doc.addons.apt = doc.addons.apt || {};
Expand All @@ -84,42 +81,72 @@ module.exports = class EmberElectronBlueprint extends Blueprint {
yawn.json = doc;
doc = yawn.json;

// add install commands -- install dependencies in electron-app project,
// and export display and launch xvfb
doc.install = doc.install || [];
let entry = doc.install.find(entry => entry.includes('yarn ') || entry.includes('npm '));
if (entry.includes('yarn')) {
doc.install.push('__yarn_install__');
} else {
doc.install.push('__npm_install__');
}

if (!doc.install.find(entry => entry.toLowerCase().includes('xvfb'))) {
// also, yawn quotes strings with certain characters in them even though
// it isn't necessary, and it makes it harder to read. So we add
// placeholders that won't be quoted and replace them in the output
// string
doc.install.push('__export_display__');
doc.install.push('__xvfb__');
}

if (!doc.install.find(entry => entry.toLowerCase().includes('xvfb'))) {
doc.install.push('__export_display__');
doc.install.push('__xvfb__');
}

// also, yawn quotes strings with certain characters in them even though
// it isn't necessary, and it makes it harder to read. So we add
// placeholders that won't be quoted and replace them in the output string
yawn.json = doc;
let output = yawn.yaml;
output = output.replace('__yarn__install__', `cd electron-app && yarn`);
output = output.replace('__npm__install__', `cd electron-app && npm install`);
output = output.replace('__export_display__', `export DISPLAY=':99.0'`);
output = output.replace('__xvfb__', 'Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &');

await writeFile('.travis.yml', output);
} catch (e) {
this.ui.writeLine(chalk.red([
`Failed to update .travis.yml. For info on manually updating your CI`,
`config read ${ciUrl}'.\nError:\n${e}`
`config read ${ciUrl}.\nError:\n${e}`
].join(' ')));
}
}

//
// Add the Electron project directory to .eslintignore. Perhaps at some point
// we can put together a good pattern for linting the Electron app, but
// currently Electron forge has no out-of-box linting, so until there's some
// better tooling elsewhere that we can integrate with, ember-electron is
// going to say "not my job"
//
async updateEslintIgnore() {
const toAppend = [
'',
'# ember-electron',
`/${electronProjectPath}/node_modules/`,
`/${electronProjectPath}/out/`,
`/${electronProjectPath}/${emberBuildDir}/`,
`/${electronProjectPath}/${emberTestBuildDir}/`
`/${electronProjectPath}/`,
].join('\n');

await this.insertIntoFile('.eslintignore', toAppend);
}

//
// Add testem-electron.js to the list of files in the rule that includes
// testem.js
//
async updateEslintRc() {
const after = /['"`]testem\.js['"`],/;
const content = ' \'testem-electron.js\',';
await this.insertIntoFile('.eslintrc.js', content, { after });
}

async createElectronProject() {
this.ui.writeLine(chalk.green(`Creating electron-forge project at './${electronProjectPath}'`));

Expand All @@ -128,28 +155,5 @@ module.exports = class EmberElectronBlueprint extends Blueprint {
interactive: true,
template: 'ember-electron/forge/template'
});

this.ui.writeLine(chalk.green(`Updating './${electronProjectPath}/package.json'`));

const keysToCopy = [
'name',
'version',
'description',
'author',
'license'
];

let packageJsonPath = path.join(electronProjectPath, 'package.json');
let packageJson = JSON.parse(await readFile(packageJsonPath));

for (let key of keysToCopy) {
if (Object.keys(this.project.pkg).includes(key)) {
packageJson[key] = this.project.pkg[key];
}
}

// special-case productName since forge creates it, but a lot of apps don't
packageJson.productName = this.project.pkg.productName || packageJson.name;
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
};
5 changes: 0 additions & 5 deletions forge/files/.eslintrc.js

This file was deleted.

60 changes: 49 additions & 11 deletions forge/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,53 @@ const readFile = promisify(require('fs').readFile);
const writeFile = promisify(require('fs').writeFile);
const rimraf = promisify(require('rimraf'));
const ncp = promisify(require('ncp'));
const { emberBuildDir, emberTestBuildDir } = require('../lib/utils/build-paths');
const {
emberBuildDir,
emberTestBuildDir
} = require('../lib/utils/build-paths');

async function updateGitIgnore(dir) {
// add our ember build directories to .gitignore
let gitIgnorePath = path.join(dir, '.gitignore');
let contents = await readFile(gitIgnorePath);
await writeFile(gitIgnorePath, [
contents.toString(),
'# Ember build',
`${emberBuildDir}/`,
`${emberTestBuildDir}/`,
''
].join('\n'));
}

async function updatePackageJson(dir) {
// add our test and test build directories and the test directory to
// electron-packager's ignores
let packageJsonPath = path.join(dir, 'package.json');
let packageJson = JSON.parse(await readFile(packageJsonPath));
packageJson.config.forge.packagerConfig.ignore = [
emberTestBuildDir,
'tests'
].map((dir) => `/${dir}(/|$)`); // these are regexes, not globs

// copy some fields from the Ember project's package.json
let parentPackageJson = JSON.parse(await readFile(path.join(dir, '../package.json')));
const keysToCopy = [
'name',
'version',
'description',
'author',
'license'
];
for (let key of keysToCopy) {
if (Object.keys(parentPackageJson).includes(key)) {
packageJson[key] = parentPackageJson[key];
}
}

// special-case productName since forge creates it, but a lot of apps don't
packageJson.productName = parentPackageJson.productName || packageJson.name;
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
}

module.exports = {
devDependencies: [
Expand All @@ -20,15 +66,7 @@ module.exports = {
// copy our initial content
await ncp(path.join(__dirname, 'files'), dir);

// add our ember build directory to .gitignore
let gitIgnorePath = path.join(dir, '.gitignore');
let contents = await readFile(gitIgnorePath);
await writeFile(gitIgnorePath, [
contents.toString(),
'# Ember build',
`${emberBuildDir}/`,
`${emberTestBuildDir}/`,
''
].join('\n'));
await updateGitIgnore(dir);
await updatePackageJson(dir);
}
};
4 changes: 2 additions & 2 deletions lib/commands/make.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const BaseCommand = require('./base');
const { emberBuildPath } = require('../utils/build-paths');
const { emberBuildPath, packageOutPath } = require('../utils/build-paths');

module.exports = BaseCommand.extend({
name: 'electron:make',
Expand All @@ -14,7 +14,7 @@ module.exports = BaseCommand.extend({
{ name: 'targets', type: String, aliases: ['t'], description: 'Override the build targets specified in your electron-forge config.' },
{ name: 'skip-package', type: Boolean, default: false, aliases: ['sp'], description: 'Skip the packaging step and use an already-packaged build from the output path.' },
{ name: 'skip-build', type: Boolean, aliases: ['sb'], description: 'If not skipping packing step, skip the Ember build step and use an already-build from the output path.' },
{ name: 'output-path', type: 'Path', default: 'electron-out', aliases: ['o'] },
{ name: 'output-path', type: 'Path', default: packageOutPath, aliases: ['o'] },
],

async run(commandOptions) {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const BaseCommand = require('./base');
const { emberBuildPath } = require('../utils/build-paths');
const { emberBuildPath, packageOutPath } = require('../utils/build-paths');

module.exports = BaseCommand.extend({
name: 'electron:package',
Expand All @@ -12,7 +12,7 @@ module.exports = BaseCommand.extend({
{ name: 'platform', type: String, aliases: ['p'] },
{ name: 'arch', type: String, aliases: ['a'] },
{ name: 'skip-build', type: Boolean, aliases: ['s'], description: 'Skip the Ember build step and use an already-build from the output path.' },
{ name: 'output-path', type: 'Path', default: 'electron-out', aliases: ['o'] },
{ name: 'output-path', type: 'Path', default: packageOutPath, aliases: ['o'] },
],

async run(commandOptions) {
Expand Down
7 changes: 6 additions & 1 deletion lib/utils/build-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ const emberBuildPath = path.join(electronProjectPath, emberBuildDir);
const emberTestBuildDir = 'ember-test';
const emberTestBuildPath = path.join(electronProjectPath, emberTestBuildDir);

const packageOutDir = 'out';
const packageOutPath = path.join(electronProjectPath, packageOutDir);

module.exports = {
electronProjectPath,
emberBuildDir,
emberBuildPath,
emberTestBuildDir,
emberTestBuildPath
emberTestBuildPath,
packageOutDir,
packageOutPath
};
Loading

0 comments on commit b820e2f

Please sign in to comment.