Skip to content

Commit

Permalink
chore: integrate prettier (electron-userland#5680)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeudan authored Mar 10, 2021
1 parent 0b03f10 commit 5d01ea6
Show file tree
Hide file tree
Showing 159 changed files with 2,540 additions and 2,485 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
rules: {
semi: "off",
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.d.ts
out
6 changes: 6 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
singleQuote: false
semi: false
arrowParens: avoid
trailingComma: all
printWidth: 250
quoteProps: as-needed
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lint": "eslint packages --ext .ts",
"lint-deps": "node ./test/out/helpers/checkDeps.js",
"pretest": "yarn compile && yarn lint && yarn lint-deps",
"prettier": "prettier \"packages/**/*.{ts, js}\" --write",
"///": "Please see https://github.com/electron-userland/electron-builder/blob/master/CONTRIBUTING.md#run-test-using-cli how to run particular test instead full (and very slow) run",
"test": "node ./test/out/helpers/runTests.js skipArtifactPublisher ALL_TESTS=isCi",
"test-all": "yarn pretest && node ./test/out/helpers/runTests.js",
Expand Down Expand Up @@ -54,12 +55,15 @@
"bluebird-lst": "^1.0.9",
"electron-builder-tslint-config": "^1.1.0",
"eslint": "^7.20.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"fs-extra": "^9.1.0",
"globby": "^11.0.2",
"husky": "4.3.8",
"jest-cli": "^26.6.3",
"jsdoc-to-markdown": "^6.0.1",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"ts-babel": "6.1.7",
"ts-jsdoc": "^3.1.1",
"typescript": "~4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/Framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ export interface PrepareApplicationStageDirectoryOptions {

export function isElectronBased(framework: Framework): boolean {
return framework.name === "electron"
}
}
44 changes: 22 additions & 22 deletions packages/app-builder-lib/src/ProtonFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,39 @@ export class ProtonFramework extends LibUiFramework {
getDefaultIcon(platform: Platform): string {
if (platform === Platform.WINDOWS) {
return getTemplatePath("icons/proton-native/proton-native.ico")
}
else if (platform === Platform.LINUX) {
} else if (platform === Platform.LINUX) {
return getTemplatePath("icons/proton-native/linux")
}
else {
} else {
return getTemplatePath("icons/proton-native/proton-native.icns")
}
}

createTransformer(): FileTransformer | null {
let babel: any
const babelOptions: any = {ast: false, sourceMaps: "inline"}
const babelOptions: any = { ast: false, sourceMaps: "inline" }
if (process.env.TEST_SET_BABEL_PRESET === "true") {
babel = require("@babel/core")
// eslint-disable-next-line @typescript-eslint/no-use-before-define
babel = testOnlyBabel(babel, babelOptions, this.version)
}
else {
} else {
try {
babel = require("babel-core")
}
catch (e) {
} catch (e) {
// babel isn't installed
log.debug(null, "don't transpile source code using Babel")
return null
}
}

log.info({options: safeStringifyJson(babelOptions, new Set<string>(["presets"]))}, "transpile source code using Babel")
log.info(
{
options: safeStringifyJson(
babelOptions,
new Set<string>(["presets"]),
),
},
"transpile source code using Babel",
)
return (file): Promise<any> | null => {
if (!(file.endsWith(".js") || file.endsWith(".jsx")) || file.includes(NODE_MODULES_PATTERN)) {
return null
Expand All @@ -57,8 +61,7 @@ export class ProtonFramework extends LibUiFramework {
return babel.transformFile(file, babelOptions, (error: Error, result: any) => {
if (error == null) {
resolve(result.code)
}
else {
} else {
reject(error)
}
})
Expand All @@ -69,24 +72,21 @@ export class ProtonFramework extends LibUiFramework {

function testOnlyBabel(babel: any, babelOptions: any, nodeVersion: string): any {
// out test dir can be located outside of electron-builder node_modules and babel cannot resolve string names of preset
babelOptions.presets = [
[require("@babel/preset-env").default, {targets: {node: nodeVersion}}],
require("@babel/preset-react"),
]
babelOptions.presets = [[require("@babel/preset-env").default, { targets: { node: nodeVersion } }], require("@babel/preset-react")]
babelOptions.plugins = [
// stage 0
require("@babel/plugin-proposal-function-bind").default,

// stage 1
require("@babel/plugin-proposal-export-default-from").default,
require("@babel/plugin-proposal-logical-assignment-operators").default,
[require("@babel/plugin-proposal-optional-chaining").default, {loose: false}],
[require("@babel/plugin-proposal-pipeline-operator").default, {proposal: "minimal"}],
[require("@babel/plugin-proposal-nullish-coalescing-operator").default, {loose: false}],
[require("@babel/plugin-proposal-optional-chaining").default, { loose: false }],
[require("@babel/plugin-proposal-pipeline-operator").default, { proposal: "minimal" }],
[require("@babel/plugin-proposal-nullish-coalescing-operator").default, { loose: false }],
require("@babel/plugin-proposal-do-expressions").default,

// stage 2
[require("@babel/plugin-proposal-decorators").default, {legacy: true}],
[require("@babel/plugin-proposal-decorators").default, { legacy: true }],
require("@babel/plugin-proposal-function-sent").default,
require("@babel/plugin-proposal-export-namespace-from").default,
require("@babel/plugin-proposal-numeric-separator").default,
Expand All @@ -95,9 +95,9 @@ function testOnlyBabel(babel: any, babelOptions: any, nodeVersion: string): any
// stage 3
require("@babel/plugin-syntax-dynamic-import").default,
require("@babel/plugin-syntax-import-meta").default,
[require("@babel/plugin-proposal-class-properties").default, {loose: false}],
[require("@babel/plugin-proposal-class-properties").default, { loose: false }],
require("@babel/plugin-proposal-json-strings").default,
]
babelOptions.babelrc = false
return babel
}
}
4 changes: 1 addition & 3 deletions packages/app-builder-lib/src/appInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export class AppInfo {

this.productName = info.config.productName || info.metadata.productName || info.metadata.name!!
this.sanitizedProductName = sanitizeFileName(this.productName)
this.productFilename = platformSpecificOptions?.executableName != null
? sanitizeFileName(platformSpecificOptions.executableName)
: this.sanitizedProductName
this.productFilename = platformSpecificOptions?.executableName != null ? sanitizeFileName(platformSpecificOptions.executableName) : this.sanitizedProductName
}

get channel(): string | null {
Expand Down
11 changes: 4 additions & 7 deletions packages/app-builder-lib/src/asar/asar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ export class AsarFilesystem {
node.size = size
if (unpacked) {
node.unpacked = true
}
else {
} else {
// electron expects string
node.offset = this.offset.toString()
if (process.platform !== "win32" && (stat.mode & 0o100)) {
if (process.platform !== "win32" && stat.mode & 0o100) {
node.executable = true
}
this.offset += node.size
Expand Down Expand Up @@ -131,8 +130,7 @@ export async function readAsar(archive: string): Promise<AsarFilesystem> {
if ((await read(fd, headerBuf, 0, size, null as any)).bytesRead !== size) {
throw new Error("Unable to read header")
}
}
finally {
} finally {
await close(fd)
}

Expand Down Expand Up @@ -161,8 +159,7 @@ async function readFileFromAsar(filesystem: AsarFilesystem, filename: string, in
try {
const offset = 8 + filesystem.headerSize + parseInt(info.offset!!, 10)
await read(fd, buffer, 0, size, offset)
}
finally {
} finally {
await close(fd)
}
return buffer
Expand Down
6 changes: 2 additions & 4 deletions packages/app-builder-lib/src/asar/asarFileChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ export async function checkFileInArchive(asarFile: string, relativeFile: string,
let fs
try {
fs = await readAsar(asarFile)
}
catch (e) {
} catch (e) {
throw error(`is corrupted: ${e}`)
}

let stat: Node | null
try {
stat = fs.getFile(relativeFile)
}
catch (e) {
} catch (e) {
const fileStat = await statOrNull(asarFile)
if (fileStat == null) {
throw error(`does not exist. Seems like a wrong configuration.`)
Expand Down
27 changes: 11 additions & 16 deletions packages/app-builder-lib/src/asar/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class AsarPackager {

const correctDirNodeUnpackedFlag = async (filePathInArchive: string, dirNode: Node) => {
for (const dir of unpackedDirs) {
if (filePathInArchive.length > (dir.length + 2) && filePathInArchive[dir.length] === path.sep && filePathInArchive.startsWith(dir)) {
if (filePathInArchive.length > dir.length + 2 && filePathInArchive[dir.length] === path.sep && filePathInArchive.startsWith(dir)) {
dirNode.unpacked = true
unpackedDirs.add(filePathInArchive)
// not all dirs marked as unpacked after first iteration - because node module dir can be marked as unpacked after processing node module dir content
Expand Down Expand Up @@ -82,7 +82,7 @@ export class AsarPackager {
const pathInArchive = path.relative(rootForAppFilesWithoutAsar, getDestinationPath(file, fileSet))

if (stat.isSymbolicLink()) {
const s = (stat as any)
const s = stat as any
this.fs.getOrCreateNode(pathInArchive).link = s.relativeLink
s.pathInArchive = pathInArchive
unpackedFileIndexSet.add(i)
Expand All @@ -105,8 +105,7 @@ export class AsarPackager {
if (fileParent !== "" && !currentDirNode.unpacked) {
if (unpackedDirs.has(fileParent)) {
currentDirNode.unpacked = true
}
else {
} else {
await correctDirNodeUnpackedFlag(fileParent, currentDirNode)
}
}
Expand Down Expand Up @@ -166,8 +165,7 @@ export class AsarPackager {
if (++fileSetIndex >= fileSets.length) {
writeStream.end()
return
}
else {
} else {
files = fileSets[fileSetIndex].files
metadata = fileSets[fileSetIndex].metadata
transformedFiles = fileSets[fileSetIndex].transformedFiles
Expand All @@ -178,8 +176,7 @@ export class AsarPackager {

if (!unpackedFileIndexSet.has(index)) {
break
}
else {
} else {
const stat = metadata.get(files[index])
if (stat != null && stat.isSymbolicLink()) {
symlink((stat as any).linkRelativeToFile, path.join(this.unpackedDest, (stat as any).pathInArchive), () => w(index + 1))
Expand All @@ -198,20 +195,19 @@ export class AsarPackager {

// https://github.com/yarnpkg/yarn/pull/3539
const stat = metadata.get(file)
if (stat != null && stat.size < (2 * 1024 * 1024)) {
if (stat != null && stat.size < 2 * 1024 * 1024) {
readFile(file)
.then(it => {
writeStream.write(it, () => w(index + 1))
})
.catch(e => reject(`Cannot read file ${file}: ${e.stack || e}`))
}
else {
} else {
const readStream = createReadStream(file)
readStream.on("error", reject)
readStream.once("end", () => w(index + 1))
readStream.on("open", () => {
readStream.pipe(writeStream, {
end: false
end: false,
})
})
}
Expand Down Expand Up @@ -256,15 +252,14 @@ async function order(filenames: Array<string>, orderingFile: string, src: string
missing += 1
}
}
log.info({coverage: ((total - missing) / total * 100)}, "ordering files in ASAR archive")
log.info({ coverage: ((total - missing) / total) * 100 }, "ordering files in ASAR archive")
return sortedFiles
}

function copyFileOrData(fileCopier: FileCopier, data: string | Buffer | undefined | null, source: string, destination: string, stats: Stats) {
if (data == null) {
return fileCopier.copy(source, destination, stats)
}
else {
} else {
return writeFile(destination, data)
}
}
}
10 changes: 4 additions & 6 deletions packages/app-builder-lib/src/asar/integrity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ export async function computeData(resourcesPath: string, options?: AsarIntegrity
for (let i = 0; i < names.length; i++) {
result[names[i]] = checksums[i]
}
return {checksums: result, ...options}
return { checksums: result, ...options }
}

function hashFile(file: string, algorithm: string = "sha512", encoding: "hex" | "base64" | "latin1" = "base64") {
return new Promise<string>((resolve, reject) => {
const hash = createHash(algorithm)
hash
.on("error", reject)
.setEncoding(encoding)
hash.on("error", reject).setEncoding(encoding)

createReadStream(file)
.on("error", reject)
.on("end", () => {
hash.end()
resolve(hash.read() as string)
})
.pipe(hash, {end: false})
.pipe(hash, { end: false })
})
}
}
Loading

0 comments on commit 5d01ea6

Please sign in to comment.