Set module: es2015 in tsconfig.json

Changelog-entry: Set module: es2015 in tsconfig.json
Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-08-04 16:17:20 +02:00
parent 281f119456
commit a3322e9fd7
21 changed files with 177 additions and 143 deletions

View File

@ -16,7 +16,7 @@
import * as _ from 'lodash';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import { Flex } from 'rendition';
import { v4 as uuidV4 } from 'uuid';
import * as flashState from '../../models/flash-state';

View File

@ -19,8 +19,7 @@ import CheckCircleSvg from '@fortawesome/fontawesome-free/svgs/solid/check-circl
import * as _ from 'lodash';
import outdent from 'outdent';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, Txt } from 'rendition';
import { progress } from '../../../../shared/messages';
import { bytesToMegabytes } from '../../../../shared/units';

View File

@ -15,10 +15,7 @@
*/
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import Button from 'rendition/dist_esm5/components/Button';
import ProgressBar from 'rendition/dist_esm5/components/ProgressBar';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, Button, ProgressBar, Txt } from 'rendition';
import { default as styled } from 'styled-components';
import { fromFlashState } from '../../modules/progress-status';

View File

@ -15,8 +15,7 @@
*/
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, Txt } from 'rendition';
import DriveSvg from '../../../assets/drive.svg';
import ImageSvg from '../../../assets/image.svg';

View File

@ -18,9 +18,7 @@ import GithubSvg from '@fortawesome/fontawesome-free/svgs/brands/github.svg';
import * as _ from 'lodash';
import * as os from 'os';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import Checkbox from 'rendition/dist_esm5/components/Checkbox';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, Checkbox, Txt } from 'rendition';
import { version, packageType } from '../../../../../package.json';
import * as settings from '../../models/settings';

View File

@ -23,12 +23,14 @@ import * as _ from 'lodash';
import { GPTPartition, MBRPartition } from 'partitioninfo';
import * as path from 'path';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import { ButtonProps } from 'rendition/dist_esm5/components/Button';
import SmallModal from 'rendition/dist_esm5/components/Modal';
import Txt from 'rendition/dist_esm5/components/Txt';
import BaseCard from 'rendition/dist_esm5/components/Card';
import Input from 'rendition/dist_esm5/components/Input';
import {
Flex,
ButtonProps,
Modal as SmallModal,
Txt,
Card as BaseCard,
Input,
} from 'rendition';
import styled from 'styled-components';
import * as errors from '../../../../shared/errors';

View File

@ -17,8 +17,7 @@
import ExclamationTriangleSvg from '@fortawesome/fontawesome-free/svgs/solid/exclamation-triangle.svg';
import { Drive as DrivelistDrive } from 'drivelist';
import * as React from 'react';
import { Flex, FlexProps } from 'rendition/dist_esm5/components/Flex';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, FlexProps, Txt } from 'rendition';
import {
getDriveImageCompatibilityStatuses,

View File

@ -18,12 +18,15 @@ import ExclamationTriangleSvg from '@fortawesome/fontawesome-free/svgs/solid/exc
import ChevronDownSvg from '@fortawesome/fontawesome-free/svgs/solid/chevron-down.svg';
import { scanner, sourceDestination } from 'etcher-sdk';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import { ModalProps } from 'rendition/dist_esm5/components/Modal';
import Txt from 'rendition/dist_esm5/components/Txt';
import Badge from 'rendition/dist_esm5/components/Badge';
import Link from 'rendition/dist_esm5/components/Link';
import Table, { TableColumn } from 'rendition/dist_esm5/components/Table';
import {
Flex,
ModalProps,
Txt,
Badge,
Link,
Table,
TableColumn,
} from 'rendition';
import styled from 'styled-components';
import {

View File

@ -86,8 +86,7 @@ const settings = _.cloneDeep(DEFAULT_SETTINGS);
async function load(): Promise<void> {
debug('load');
// Use exports.readAll() so it can be mocked in tests
const loadedSettings = await exports.readAll();
const loadedSettings = await readAll();
_.assign(settings, loadedSettings);
}

View File

@ -136,16 +136,13 @@ interface FlashResults {
/**
* @summary Perform write operation
*
* @description
* This function is extracted for testing purposes.
*/
export async function performWrite(
async function performWrite(
image: string,
drives: DrivelistDrive[],
onProgress: sdk.multiWrite.OnProgressFunction,
source: SourceOptions,
): Promise<{ cancelled?: boolean }> {
): Promise<FlashResults> {
let cancelled = false;
ipc.serve();
const {
@ -264,6 +261,8 @@ export async function flash(
image: string,
drives: DrivelistDrive[],
source: SourceOptions,
// This function is a parameter so it can be mocked in tests
write = performWrite,
): Promise<void> {
if (flashState.isFlashing()) {
throw new Error('There is already a flash in progress');
@ -288,8 +287,7 @@ export async function flash(
analytics.logEvent('Flash', analyticsData);
try {
// Using it from exports so it can be mocked during tests
const result = await exports.performWrite(
const result = await write(
image,
drives,
flashState.setProgressState,

View File

@ -50,9 +50,9 @@ export const currentWindow = electron.remote.getCurrentWindow();
*/
export function set(state: FlashState) {
if (state.percentage != null) {
exports.currentWindow.setProgressBar(percentageToFloat(state.percentage));
currentWindow.setProgressBar(percentageToFloat(state.percentage));
}
exports.currentWindow.setTitle(getWindowTitle(state));
currentWindow.setTitle(getWindowTitle(state));
}
/**
@ -60,6 +60,6 @@ export function set(state: FlashState) {
*/
export function clear() {
// Passing 0 or null/undefined doesn't work.
exports.currentWindow.setProgressBar(-1);
exports.currentWindow.setTitle(getWindowTitle(undefined));
currentWindow.setProgressBar(-1);
currentWindow.setTitle(getWindowTitle(undefined));
}

View File

@ -31,8 +31,7 @@ const execAsync = promisify(exec);
/**
* @summary Returns wmic's output for network drives
*/
export async function getWmicNetworkDrivesOutput(): Promise<string> {
// Exported for tests.
async function getWmicNetworkDrivesOutput(): Promise<string> {
// When trying to read wmic's stdout directly from node, it is encoded with the current
// console codepage (depending on the computer).
// Decoding this would require getting this codepage somehow and using iconv as node
@ -66,9 +65,10 @@ export async function getWmicNetworkDrivesOutput(): Promise<string> {
/**
* @summary returns a Map of drive letter -> network locations on Windows: 'Z:' -> '\\\\192.168.0.1\\Public'
*/
async function getWindowsNetworkDrives(): Promise<Map<string, string>> {
// Use getWindowsNetworkDrives from "exports." so it can be mocked in tests
const result = await exports.getWmicNetworkDrivesOutput();
async function getWindowsNetworkDrives(
getWmicOutput: () => Promise<string>,
): Promise<Map<string, string>> {
const result = await getWmicOutput();
const couples: Array<[string, string]> = chain(result)
.split('\n')
// Remove header line
@ -97,13 +97,15 @@ async function getWindowsNetworkDrives(): Promise<Map<string, string>> {
*/
export async function replaceWindowsNetworkDriveLetter(
filePath: string,
// getWmicOutput is a parameter so it can be replaced in tests
getWmicOutput = getWmicNetworkDrivesOutput,
): Promise<string> {
let result = filePath;
if (platform() === 'win32') {
const matches = /^([A-Z]+:)\\(.*)$/.exec(filePath);
if (matches !== null) {
const [, drive, relativePath] = matches;
const drives = await getWindowsNetworkDrives();
const drives = await getWindowsNetworkDrives(getWmicOutput);
const location = drives.get(drive);
if (location !== undefined) {
result = `${location}\\${relativePath}`;

View File

@ -16,7 +16,7 @@
import { scanner } from 'etcher-sdk';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import { Flex } from 'rendition';
import { TargetSelector } from '../../components/target-selector/target-selector-button';
import { TargetSelectorModal } from '../../components/target-selector/target-selector-modal';
import {

View File

@ -18,9 +18,7 @@ import CircleSvg from '@fortawesome/fontawesome-free/svgs/solid/circle.svg';
import * as _ from 'lodash';
import * as path from 'path';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import Modal from 'rendition/dist_esm5/components/Modal';
import Txt from 'rendition/dist_esm5/components/Txt';
import { Flex, Modal, Txt } from 'rendition';
import * as constraints from '../../../../shared/drive-constraints';
import * as messages from '../../../../shared/messages';

View File

@ -21,7 +21,7 @@ import { sourceDestination } from 'etcher-sdk';
import * as _ from 'lodash';
import * as path from 'path';
import * as React from 'react';
import { Flex } from 'rendition/dist_esm5/components/Flex';
import { Flex } from 'rendition';
import styled from 'styled-components';
import { FeaturedProject } from '../../components/featured-project/featured-project';

View File

@ -15,12 +15,16 @@
*/
import * as React from 'react';
import { Flex, FlexProps } from 'rendition/dist_esm5/components/Flex';
import Button, { ButtonProps } from 'rendition/dist_esm5/components/Button';
import ModalBase from 'rendition/dist_esm5/components/Modal';
import Provider from 'rendition/dist_esm5/components/Provider';
import Txt from 'rendition/dist_esm5/components/Txt';
import renditionTheme from 'rendition/dist_esm5/theme';
import {
Flex,
FlexProps,
Button,
ButtonProps,
Modal as ModalBase,
Provider,
Txt,
Theme as renditionTheme,
} from 'rendition';
import styled from 'styled-components';
import { space } from 'styled-system';

View File

@ -17,7 +17,6 @@
import { expect } from 'chai';
import { Drive as DrivelistDrive } from 'drivelist';
import { sourceDestination } from 'etcher-sdk';
import * as _ from 'lodash';
import * as ipc from 'node-ipc';
import { assert, SinonStub, stub } from 'sinon';
@ -39,7 +38,7 @@ describe('Browser: imageWriter', () => {
let performWriteStub: SinonStub;
beforeEach(() => {
performWriteStub = stub(imageWriter, 'performWrite');
performWriteStub = stub();
performWriteStub.returns(
Promise.resolve({
cancelled: false,
@ -49,52 +48,56 @@ describe('Browser: imageWriter', () => {
});
afterEach(() => {
performWriteStub.restore();
performWriteStub.reset();
});
it('should set flashing to false when done', () => {
it('should set flashing to false when done', async () => {
flashState.unsetFlashingFlag({
cancelled: false,
sourceChecksum: '1234',
});
imageWriter.flash(imagePath, [fakeDrive], sourceOptions).finally(() => {
try {
await imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
);
} catch {
// noop
} finally {
expect(flashState.isFlashing()).to.be.false;
});
}
});
it('should prevent writing more than once', () => {
it('should prevent writing more than once', async () => {
flashState.unsetFlashingFlag({
cancelled: false,
sourceChecksum: '1234',
});
const writing = imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
);
imageWriter.flash(imagePath, [fakeDrive], sourceOptions).catch(_.noop);
writing.finally(() => {
assert.calledOnce(performWriteStub);
});
});
it('should reject the second flash attempt', () => {
imageWriter.flash(imagePath, [fakeDrive], sourceOptions);
let rejectError: Error;
imageWriter
.flash(imagePath, [fakeDrive], sourceOptions)
.catch((error) => {
rejectError = error;
})
.finally(() => {
expect(rejectError).to.be.an.instanceof(Error);
expect(rejectError!.message).to.equal(
'There is already a flash in progress',
);
});
try {
await Promise.all([
imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
),
imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
),
]);
assert.fail('Writing twice should fail');
} catch (error) {
expect(error.message).to.equal(
'There is already a flash in progress',
);
}
});
});
@ -102,51 +105,63 @@ describe('Browser: imageWriter', () => {
let performWriteStub: SinonStub;
beforeEach(() => {
performWriteStub = stub(imageWriter, 'performWrite');
performWriteStub = stub();
const error: Error & { code?: string } = new Error('write error');
error.code = 'FOO';
performWriteStub.returns(Promise.reject(error));
});
afterEach(() => {
performWriteStub.restore();
performWriteStub.reset();
});
it('should set flashing to false when done', () => {
imageWriter
.flash(imagePath, [fakeDrive], sourceOptions)
.catch(_.noop)
.finally(() => {
expect(flashState.isFlashing()).to.be.false;
});
it('should set flashing to false when done', async () => {
try {
await imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
);
} catch {
// noop
} finally {
expect(flashState.isFlashing()).to.be.false;
}
});
it('should set the error code in the flash results', () => {
imageWriter
.flash(imagePath, [fakeDrive], sourceOptions)
.catch(_.noop)
.finally(() => {
const flashResults = flashState.getFlashResults();
expect(flashResults.errorCode).to.equal('FOO');
});
it('should set the error code in the flash results', async () => {
try {
await imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
);
} catch {
// noop
} finally {
const flashResults = flashState.getFlashResults();
expect(flashResults.errorCode).to.equal('FOO');
}
});
it('should be rejected with the error', () => {
it('should be rejected with the error', async () => {
flashState.unsetFlashingFlag({
cancelled: false,
sourceChecksum: '1234',
});
let rejection: Error;
imageWriter
.flash(imagePath, [fakeDrive], sourceOptions)
.catch((error) => {
rejection = error;
})
.finally(() => {
expect(rejection).to.be.an.instanceof(Error);
expect(rejection!.message).to.equal('write error');
});
try {
await imageWriter.flash(
imagePath,
[fakeDrive],
sourceOptions,
performWriteStub,
);
} catch (error) {
expect(error).to.be.an.instanceof(Error);
expect(error.message).to.equal('write error');
}
});
});
});

View File

@ -17,37 +17,34 @@
import { expect } from 'chai';
import { promises as fs } from 'fs';
import * as os from 'os';
import { env } from 'process';
import { SinonStub, stub } from 'sinon';
import * as wnd from '../../../lib/gui/app/os/windows-network-drives';
function mockGetWmicOutput() {
return fs.readFile('tests/data/wmic-output.txt', {
encoding: 'ucs2',
});
}
describe('Network drives on Windows', () => {
let osPlatformStub: SinonStub;
let outputStub: SinonStub;
let oldSystemRoot: string | undefined;
before(async () => {
osPlatformStub = stub(os, 'platform');
osPlatformStub.returns('win32');
const wmicOutput = await fs.readFile('tests/data/wmic-output.txt', {
encoding: 'ucs2',
});
outputStub = stub(wnd, 'getWmicNetworkDrivesOutput');
outputStub.resolves(wmicOutput);
oldSystemRoot = env.SystemRoot;
env.SystemRoot = 'C:\\Windows';
});
it('should parse network drive mapping on Windows', async () => {
expect(
await wnd.replaceWindowsNetworkDriveLetter('Z:\\some-folder\\some-file'),
await wnd.replaceWindowsNetworkDriveLetter(
'Z:\\some-folder\\some-file',
mockGetWmicOutput,
),
).to.equal('\\\\192.168.1.1\\Publicé\\some-folder\\some-file');
});
after(() => {
osPlatformStub.restore();
outputStub.restore();
env.SystemRoot = oldSystemRoot;
});
});

View File

@ -4,15 +4,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"module": "commonjs",
"target": "es2019",
"jsx": "react",
"typeRoots": ["./node_modules/@types", "./typings"],
"importHelpers": true,
"allowSyntheticDefaultImports": true
},
"include": [
"lib/**/*.ts",
"node_modules/electron/**/*.d.ts"
]
"typeRoots": ["./node_modules/@types", "./typings"]
}
}

19
tsconfig.webpack.json Normal file
View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"module": "es2015",
"target": "es2019",
"jsx": "react",
"typeRoots": ["./node_modules/@types", "./typings"],
"importHelpers": true,
"allowSyntheticDefaultImports": true
},
"include": [
"lib/**/*.ts",
"node_modules/electron/**/*.d.ts"
]
}

View File

@ -24,7 +24,11 @@ import * as path from 'path';
import { env } from 'process';
import * as SimpleProgressWebpackPlugin from 'simple-progress-webpack-plugin';
import * as TerserPlugin from 'terser-webpack-plugin';
import { BannerPlugin, NormalModuleReplacementPlugin } from 'webpack';
import {
BannerPlugin,
IgnorePlugin,
NormalModuleReplacementPlugin,
} from 'webpack';
/**
* Don't webpack package.json as mixpanel & sentry tokens
@ -135,7 +139,14 @@ const commonConfig = {
},
{
test: /\.tsx?$/,
use: 'ts-loader',
use: [
{
loader: 'ts-loader',
options: {
configFile: 'tsconfig.webpack.json',
},
},
],
},
// don't import WeakMap polyfill in deep-map-keys (required in corvus)
replace(/node_modules\/deep-map-keys\/lib\/deep-map-keys\.js$/, {
@ -237,6 +248,8 @@ const commonConfig = {
extensions: ['.node', '.js', '.json', '.ts', '.tsx'],
},
plugins: [
// Rendition imports highlight.js default.css file, we don't need it
new IgnorePlugin({ resourceRegExp: /\.css$/ }),
new SimpleProgressWebpackPlugin({
format: process.env.WEBPACK_PROGRESS || 'verbose',
}),