Skip to content
Permalink
Browse files

refactor(template-webpack): extract rewrite template file function

  • Loading branch information
malept committed Jul 15, 2019
1 parent cd63874 commit 5b5860632178f2972aafd24560b4aa6a19ea9add
Showing with 23 additions and 8 deletions.
  1. +12 −2 packages/api/core/test/slow/api_spec_slow.ts
  2. +11 −6 packages/template/webpack/src/WebpackTemplate.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);
});
@@ -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'));
});
}
}

0 comments on commit 5b58606

Please sign in to comment.
You can’t perform that action at this time.