Skip to content

Commit

Permalink
refactor(template-webpack): extract rewrite template file function
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Jul 15, 2019
1 parent cd63874 commit 5b58606
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 12 additions & 2 deletions packages/api/core/test/slow/api_spec_slow.ts
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand Down
17 changes: 11 additions & 6 deletions packages/template/webpack/src/WebpackTemplate.ts
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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'));
});
}
}
Expand Down

0 comments on commit 5b58606

Please sign in to comment.