This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor init module for improved testability * Add unit tests for parts of the init command module Co-authored-by: Erik Marks <[email protected]>
- Loading branch information
1 parent
0af6a4f
commit bfdc42d
Showing
6 changed files
with
596 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { promises as fs } from 'fs'; | ||
import { CONFIG_PATHS, logError, closePrompt } from '../../utils'; | ||
import { YargsArgs } from '../../types/yargs'; | ||
import template from './initTemplate.json'; | ||
import { asyncPackageInit, validateEmptyDir, buildWeb3Wallet } from './initUtils'; | ||
|
||
const CONFIG_PATH = CONFIG_PATHS[0]; | ||
|
||
export async function initHandler(argv: YargsArgs) { | ||
console.log(`Init: Begin building 'package.json'\n`); | ||
|
||
const pkg = await asyncPackageInit(); | ||
|
||
await validateEmptyDir(); | ||
|
||
console.log(`\nInit: Set 'package.json' web3Wallet properties\n`); | ||
|
||
const [web3Wallet, _newArgs] = await buildWeb3Wallet(argv); | ||
const newArgs = _newArgs as YargsArgs; | ||
pkg.web3Wallet = web3Wallet; | ||
|
||
try { | ||
await fs.writeFile('package.json', `${JSON.stringify(pkg, null, 2)}\n`); | ||
} catch (err) { | ||
logError(`Init Error: Fatal: Failed to write package.json`, err); | ||
process.exit(1); | ||
} | ||
|
||
console.log(`\nInit: 'package.json' web3Wallet properties set successfully!`); | ||
|
||
// write main js entry file | ||
const { main } = pkg; | ||
if (main !== undefined) { | ||
newArgs.src = main; | ||
try { | ||
await fs.writeFile(main, template.js); | ||
console.log(`Init: Wrote main entry file '${main}'`); | ||
} catch (err) { | ||
logError(`Init Error: Fatal: Failed to write main .js file '${main}'`, err); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
// write index.html | ||
try { | ||
await fs.writeFile('index.html', template.html.toString() | ||
.replace(/_PORT_/gu, newArgs.port.toString() || argv.port.toString())); | ||
console.log(`Init: Wrote 'index.html' file`); | ||
} catch (err) { | ||
logError(`Init Error: Fatal: Failed to write index.html file`, err); | ||
process.exit(1); | ||
} | ||
|
||
// write config file | ||
try { | ||
await fs.writeFile(CONFIG_PATH, JSON.stringify(newArgs, null, 2)); | ||
console.log(`Init: Wrote '${CONFIG_PATH}' config file`); | ||
} catch (err) { | ||
logError(`Init Error: Failed to write '${CONFIG_PATH}' file`, err); | ||
} | ||
|
||
closePrompt(); | ||
return { ...argv, ...newArgs }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const init = require('../../../dist/src/cmds/init'); | ||
const initializeModule = require('../../../dist/src/cmds/init/initHandler'); | ||
const buildModule = require('../../../dist/src/cmds/build'); | ||
|
||
describe('init module', () => { | ||
it('console logs if successful', async () => { | ||
const mockArgv = { foo: 'bar' }; | ||
const initHandlerMock = jest.spyOn(initializeModule, 'initHandler').mockImplementation(() => mockArgv); | ||
const buildMock = jest.spyOn(buildModule, 'build').mockImplementation(); | ||
jest.spyOn(console, 'log').mockImplementation(); | ||
|
||
await init.handler({ ...mockArgv }); | ||
expect(initHandlerMock).toHaveBeenCalledWith(mockArgv); | ||
expect(buildMock).toHaveBeenCalledWith({ foo: 'bar', manifest: false, eval: true }); | ||
expect(global.console.log).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
Oops, something went wrong.