From 5bf797a80991f9e57e33c4407ccf958b1a8b03b9 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 15 Jul 2019 08:55:30 -0700 Subject: [PATCH 1/4] refactor(template-webpack): extract copy template file function --- packages/api/core/test/slow/api_spec_slow.ts | 14 +++++++++++++- packages/template/webpack/src/WebpackTemplate.ts | 13 +++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/api/core/test/slow/api_spec_slow.ts b/packages/api/core/test/slow/api_spec_slow.ts index f7d30cfa0a..600288d034 100644 --- a/packages/api/core/test/slow/api_spec_slow.ts +++ b/packages/api/core/test/slow/api_spec_slow.ts @@ -104,7 +104,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { }); }); - describe('init (with built-in templater)', () => { + describe.only('init (with built-in templater)', () => { before(ensureTestDirIsNonexistent); it('should succeed in initializing', async () => { @@ -125,6 +125,18 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { expect(Object.keys(require(path.resolve(dir, 'package.json')).devDependencies)).to.contain('@electron-forge/plugin-webpack'); }); + it('should copy the appropriate template files', async () => { + const expectedFiles = [ + 'webpack.main.config.js', + 'webpack.renderer.config.js', + 'webpack.rules.js', + path.join('src', 'renderer.js'), + ]; + for (const filename of expectedFiles) { + await expectProjectPathExists(filename, 'file'); + } + }); + after(async () => { await fs.remove(dir); }); diff --git a/packages/template/webpack/src/WebpackTemplate.ts b/packages/template/webpack/src/WebpackTemplate.ts index 99de31763f..e53b1d4ced 100644 --- a/packages/template/webpack/src/WebpackTemplate.ts +++ b/packages/template/webpack/src/WebpackTemplate.ts @@ -6,6 +6,11 @@ import path from 'path'; const currentVersion = require('../package').version; +const copyTemplateFile = async (destDir: string, basename: string) => { + const templateDir = path.resolve(__dirname, '..', 'tmpl'); + await fs.copy(path.join(templateDir, basename), path.resolve(destDir, basename)); +} + class WebpackTemplate implements ForgeTemplate { public devDependencies = [ `@electron-forge/plugin-webpack@${currentVersion}`, @@ -41,10 +46,10 @@ class WebpackTemplate implements ForgeTemplate { }); }); await asyncOra('Setting up webpack configuration', async () => { - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.main.config.js'), path.resolve(directory, 'webpack.main.config.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.renderer.config.js'), path.resolve(directory, 'webpack.renderer.config.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.rules.js'), path.resolve(directory, 'webpack.rules.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'renderer.js'), path.resolve(directory, 'src', 'renderer.js')); + await copyTemplateFile(directory, 'webpack.main.config.js'); + await copyTemplateFile(directory, 'webpack.renderer.config.js'); + await copyTemplateFile(directory, 'webpack.rules.js'); + await copyTemplateFile(path.join(directory, 'src'), 'renderer.js'); let indexContents = await fs.readFile(path.resolve(directory, 'src', 'index.js'), 'utf8'); indexContents = indexContents.split('\n').map((line) => { if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);'; From 414e4e9db44bf994a5ced4b18b88ca0461ca1d2c Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 15 Jul 2019 09:15:29 -0700 Subject: [PATCH 2/4] refactor(template-webpack): extract rewrite template file function --- packages/api/core/test/slow/api_spec_slow.ts | 14 ++++++++++++-- .../template/webpack/src/WebpackTemplate.ts | 17 +++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/api/core/test/slow/api_spec_slow.ts b/packages/api/core/test/slow/api_spec_slow.ts index 600288d034..419a3c4a67 100644 --- a/packages/api/core/test/slow/api_spec_slow.ts +++ b/packages/api/core/test/slow/api_spec_slow.ts @@ -40,8 +40,12 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { }); }; - const expectProjectPathExists = async (subPath: string, pathType: string) => { - expect(await fs.pathExists(path.resolve(dir, subPath)), `the ${subPath} ${pathType} should exist`).to.equal(true); + const expectProjectPathExists = async (subPath: string, pathType: string, exists = true) => { + expect(await fs.pathExists(path.resolve(dir, subPath)), `the ${subPath} ${pathType} should exist`).to.equal(exists); + }; + + const expectProjectPathNotExists = async (subPath: string, pathType: string, exists = true) => { + expectProjectPathExists(subPath, pathType, false); }; describe('init', () => { @@ -137,6 +141,12 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { } }); + it('should move and rewrite the main process file', async () => { + await expectProjectPathNotExists(path.join('src', 'index.js'), 'file'); + await expectProjectPathExists(path.join('src', 'main.js'), 'file'); + expect(await fs.readFile(path.join(dir, 'src', 'main.js'))).to.match(/MAIN_WINDOW_WEBPACK_ENTRY/); + }); + after(async () => { await fs.remove(dir); }); diff --git a/packages/template/webpack/src/WebpackTemplate.ts b/packages/template/webpack/src/WebpackTemplate.ts index e53b1d4ced..5606311df3 100644 --- a/packages/template/webpack/src/WebpackTemplate.ts +++ b/packages/template/webpack/src/WebpackTemplate.ts @@ -11,6 +11,14 @@ const copyTemplateFile = async (destDir: string, basename: string) => { await fs.copy(path.join(templateDir, basename), path.resolve(destDir, basename)); } +const updateFileByLine = async (inputPath: string, lineHandler: (line: string) => string, outputPath: string | undefined = undefined) => { + const fileContents = (await fs.readFile(inputPath, 'utf8')).split('\n').map(lineHandler).join('\n'); + await fs.writeFile(outputPath || inputPath, fileContents); + if (outputPath !== undefined) { + await fs.remove(inputPath); + } +} + class WebpackTemplate implements ForgeTemplate { public devDependencies = [ `@electron-forge/plugin-webpack@${currentVersion}`, @@ -50,14 +58,11 @@ class WebpackTemplate implements ForgeTemplate { await copyTemplateFile(directory, 'webpack.renderer.config.js'); await copyTemplateFile(directory, 'webpack.rules.js'); await copyTemplateFile(path.join(directory, 'src'), 'renderer.js'); - let indexContents = await fs.readFile(path.resolve(directory, 'src', 'index.js'), 'utf8'); - indexContents = indexContents.split('\n').map((line) => { + + await updateFileByLine(path.resolve(directory, 'src', 'index.js'), (line) => { if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);'; - if (line.includes('link rel="stylesheet"')) return ''; return line; - }).join('\n'); - await fs.writeFile(path.resolve(directory, 'src', 'main.js'), indexContents); - await fs.remove(path.resolve(directory, 'src', 'index.js')); + }, path.resolve(directory, 'src', 'main.js')); }); } } From 567e79f19ecdd47879ac5722d326cfe6d9264784 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 15 Jul 2019 10:40:57 -0700 Subject: [PATCH 3/4] fix(template-webpack): properly remove stylesheet link from index.html --- packages/api/core/test/slow/api_spec_slow.ts | 8 ++++++-- packages/template/webpack/src/WebpackTemplate.ts | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/api/core/test/slow/api_spec_slow.ts b/packages/api/core/test/slow/api_spec_slow.ts index 419a3c4a67..639674207d 100644 --- a/packages/api/core/test/slow/api_spec_slow.ts +++ b/packages/api/core/test/slow/api_spec_slow.ts @@ -108,7 +108,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { }); }); - describe.only('init (with built-in templater)', () => { + describe('init (with built-in templater)', () => { before(ensureTestDirIsNonexistent); it('should succeed in initializing', async () => { @@ -144,7 +144,11 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { it('should move and rewrite the main process file', async () => { await expectProjectPathNotExists(path.join('src', 'index.js'), 'file'); await expectProjectPathExists(path.join('src', 'main.js'), 'file'); - expect(await fs.readFile(path.join(dir, 'src', 'main.js'))).to.match(/MAIN_WINDOW_WEBPACK_ENTRY/); + expect((await fs.readFile(path.join(dir, 'src', 'main.js'))).toString()).to.match(/MAIN_WINDOW_WEBPACK_ENTRY/); + }); + + it('should remove the stylesheet link from the HTML file', async () => { + expect((await fs.readFile(path.join(dir, 'src', 'index.html'))).toString()).to.not.match(/link rel="stylesheet"/); }); after(async () => { diff --git a/packages/template/webpack/src/WebpackTemplate.ts b/packages/template/webpack/src/WebpackTemplate.ts index 5606311df3..bb980724cf 100644 --- a/packages/template/webpack/src/WebpackTemplate.ts +++ b/packages/template/webpack/src/WebpackTemplate.ts @@ -63,6 +63,11 @@ class WebpackTemplate implements ForgeTemplate { if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);'; return line; }, path.resolve(directory, 'src', 'main.js')); + + await updateFileByLine(path.resolve(directory, 'src', 'index.html'), (line) => { + if (line.includes('link rel="stylesheet"')) return ''; + return line; + }); }); } } From 700c573dd9625bb280b1de3d9e70346f2d77dd42 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 15 Jul 2019 12:11:17 -0700 Subject: [PATCH 4/4] Fix lint warnings --- packages/api/core/test/slow/api_spec_slow.ts | 2 +- packages/template/webpack/src/WebpackTemplate.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/api/core/test/slow/api_spec_slow.ts b/packages/api/core/test/slow/api_spec_slow.ts index 639674207d..79dc16e594 100644 --- a/packages/api/core/test/slow/api_spec_slow.ts +++ b/packages/api/core/test/slow/api_spec_slow.ts @@ -44,7 +44,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { expect(await fs.pathExists(path.resolve(dir, subPath)), `the ${subPath} ${pathType} should exist`).to.equal(exists); }; - const expectProjectPathNotExists = async (subPath: string, pathType: string, exists = true) => { + const expectProjectPathNotExists = async (subPath: string, pathType: string) => { expectProjectPathExists(subPath, pathType, false); }; diff --git a/packages/template/webpack/src/WebpackTemplate.ts b/packages/template/webpack/src/WebpackTemplate.ts index bb980724cf..d71398ab07 100644 --- a/packages/template/webpack/src/WebpackTemplate.ts +++ b/packages/template/webpack/src/WebpackTemplate.ts @@ -9,15 +9,19 @@ const currentVersion = require('../package').version; const copyTemplateFile = async (destDir: string, basename: string) => { const templateDir = path.resolve(__dirname, '..', 'tmpl'); await fs.copy(path.join(templateDir, basename), path.resolve(destDir, basename)); -} +}; -const updateFileByLine = async (inputPath: string, lineHandler: (line: string) => string, outputPath: string | undefined = undefined) => { +const updateFileByLine = async ( + inputPath: string, + lineHandler: (line: string) => string, + outputPath: string | undefined = undefined, +) => { const fileContents = (await fs.readFile(inputPath, 'utf8')).split('\n').map(lineHandler).join('\n'); await fs.writeFile(outputPath || inputPath, fileContents); if (outputPath !== undefined) { await fs.remove(inputPath); } -} +}; class WebpackTemplate implements ForgeTemplate { public devDependencies = [