Skip to content

Commit

Permalink
chore: use depcheck to lint unused/incorrect dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Oct 24, 2016
1 parent 81c6bdf commit 3eb2752
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 132 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ language: c
cache:
directories:
- node_modules
- nsis-auto-updater/node_modules
- $HOME/.electron

before_install:
Expand Down
4 changes: 3 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ install:
- ps: Install-Product node 6 x64
- choco install -i yarn
- refreshenv
- yarn install
# unknown yarn error
- npm install
- npm prune

build: off

Expand Down
2 changes: 1 addition & 1 deletion nsis-auto-updater/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-auto-updater",
"version": "0.0.3",
"version": "0.0.4",
"description": "NSIS Auto Updater",
"main": "out/nsis-auto-updater/src/main.js",
"author": "Vladimir Krivosheev",
Expand Down
6 changes: 3 additions & 3 deletions nsis-auto-updater/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

fs-extra-p@^1.1.10:
version "1.1.10"
resolved "https://registry.yarnpkg.com/fs-extra-p/-/fs-extra-p-1.1.10.tgz#10ff1f1091454b99d9cd6c6eaad84ca8ae85d977"
fs-extra-p@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/fs-extra-p/-/fs-extra-p-1.2.0.tgz#16abed58ec63219cf9244a5a54ba200d4864b347"
dependencies:
bluebird "^3.4.6"
fs-extra-tf "^0.30.3"
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"scripts": {
"compile": "ts-babel . nsis-auto-updater test",
"lint": "tslint 'src/**/*.ts' 'test/src/**/*.ts' 'nsis-auto-updater/src/**/*.ts'",
"pretest": "npm run compile && npm run lint && npm run pack-updater",
"pretest": "yarn run compile && yarn run lint",
"check-deps": "node ./test/out/helpers/checkDeps.js",
"test": "node ./test/out/helpers/runTests.js",
"test-linux": "docker run --rm -ti -v ${PWD}:/project -v ${PWD##*/}-node-modules:/project/node_modules -v ~/.electron:/root/.electron electronuserland/electron-builder:wine /test.sh",
"pack-updater": "cd nsis-auto-updater && npm install --production --no-bin-links && cd ..",
"pack-updater": "cd nsis-auto-updater && yarn --production && cd ..",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"//": "Update wiki if docs changed. Update only if functionalily are generally available (latest release, not next)",
"update-wiki": "git subtree split -b wiki --prefix docs/ && git push -f wiki wiki:master",
Expand Down Expand Up @@ -76,7 +77,6 @@
"lodash.template": "^4.4.0",
"mime": "^1.3.4",
"minimatch": "^3.0.3",
"nodent-runtime": "^3.0.1",
"normalize-package-data": "^2.3.5",
"parse-color": "^1.0.0",
"plist": "^2.0.1",
Expand All @@ -86,15 +86,14 @@
"read-installed": "^4.0.3",
"sanitize-filename": "^1.6.1",
"semver": "^5.3.0",
"source-map-support": "^0.4.4",
"source-map-support": "^0.4.5",
"tunnel-agent": "^0.4.3",
"update-notifier": "^1.0.2",
"uuid-1345": "^0.99.6",
"yargs": "^6.0.0"
"yargs": "^6.3.0"
},
"devDependencies": {
"@develar/babel-plugin-transform-inline-imports-commonjs": "^1.0.0",
"@develar/semantic-release": "^6.3.6",
"@develar/semantic-release": "^6.3.7",
"@develar/types": "^1.0.1",
"@types/source-map-support": "^0.2.28",
"ava-tf": "^0.16.4",
Expand All @@ -103,7 +102,9 @@
"babel-plugin-transform-es2015-destructuring": "^6.16.0",
"babel-plugin-transform-es2015-parameters": "^6.17.0",
"babel-plugin-transform-es2015-spread": "^6.8.0",
"babel-plugin-transform-inline-imports-commonjs": "^1.2.0",
"decompress-zip": "^0.3.0",
"depcheck": "^0.6.4",
"diff": "^3.0.1",
"husky": "^0.11.9",
"json8": "^0.9.2",
Expand All @@ -127,7 +128,7 @@
"transform-es2015-spread",
"transform-es2015-destructuring",
"array-includes",
"@develar/babel-plugin-transform-inline-imports-commonjs"
"transform-inline-imports-commonjs"
]
},
"ava": {
Expand Down
6 changes: 3 additions & 3 deletions src/util/tmp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { tmpdir } from "os"
import { remove, mkdirs, removeSync } from "fs-extra-p"
import * as path from "path"
import { getTempName, use } from "./util"
import { getTempName } from "./util"
import BluebirdPromise from "bluebird"
import { warn } from "./log"

const mkdtemp: any | null = use(require("fs").mkdtemp, it => BluebirdPromise.promisify(it))
const mkdtemp: any | null = require("fs-extra-p").mkdtemp

export class TmpDir {
private tmpFileCounter = 0
Expand All @@ -21,7 +21,7 @@ export class TmpDir {
promise = mkdirs(dir, {mode: 448}).thenReturn(dir)
}
else {
promise = mkdtemp(`${path.join(tmpdir(), "electron-builder")}-`)
promise = mkdtemp(`${path.join(process.env.TEST_DIR || tmpdir(), "electron-builder")}-`)
}

this.tempDirectoryPromise = promise
Expand Down
6 changes: 6 additions & 0 deletions test/src/helpers/avaEx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import test from "ava-tf"
import BluebirdPromise from "bluebird"

BluebirdPromise.config({
longStackTraces: true,
cancellation: true
})

/* tslint:disable:no-invalid-this no-namespace */

Expand Down
63 changes: 63 additions & 0 deletions test/src/helpers/checkDeps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as path from "path"
import { readJson } from "fs-extra-p"
import BluebirdPromise from "bluebird"
import depCheck, { DepCheckResult } from "depcheck"

const printErrorAndExit = require("../../../out/util/promise").printErrorAndExit

const knownUnusedDevDependencies = new Set([
"@develar/types",
"@types/source-map-support",
"ava-tf",
"decompress-zip",
"diff",
"husky",
"json8",
"path-sort",
"typescript",
"depcheck"
])

async function main(): Promise<void> {
const projectDir = path.join(__dirname, "..", "..", "..")

console.log(`Checking ${projectDir}`)

const result = await new BluebirdPromise<DepCheckResult>(function (resolve) {
depCheck(projectDir, {
ignoreDirs: [
"out", "test", "docs", "typings", "docker", "certs", "templates", "nsis-auto-updater", ".idea", ".github",
],
}, resolve)
})

if (result.dependencies.length > 0) {
throw new Error(`Unused dependencies: ${JSON.stringify(result.dependencies, null, 2)}`)
}

const unusedDevDependencies = result.devDependencies.filter(it => !knownUnusedDevDependencies.has(it))
if (unusedDevDependencies.length > 0) {
throw new Error(`Unused devDependencies: ${JSON.stringify(unusedDevDependencies, null, 2)}`)
}

if (result.missing.length > 0) {
throw new Error(`Missing devDependencies: ${JSON.stringify(result.missing, null, 2)}`)
}

const packageData = await readJson(path.join(projectDir, "package.json"))
for (let name of Object.keys(packageData.devDependencies)) {
const usages = result.using[name]
if (usages == null || usages.length === 0) {
continue
}

for (let file of usages) {
if (file.startsWith(path.join(projectDir, "src") + path.sep)) {
throw new Error(`Dev dependency ${name} is used in the sources`)
}
}
}
}

main()
.catch(printErrorAndExit)
1 change: 0 additions & 1 deletion test/src/helpers/packTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { AssertContext } from "ava-tf"
import { SquirrelWindowsOptions } from "out/options/winOptions"

if (process.env.TRAVIS !== "true") {
// we don't use CircleCI, so, we can safely set this env
process.env.CIRCLE_BUILD_NUM = 42
}

Expand Down
83 changes: 4 additions & 79 deletions test/src/helpers/runTests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { spawn } from "child_process"
import * as path from "path"
import BluebirdPromise from "bluebird"
import { copy, emptyDir, outputFile, readdir, readFileSync, readJson, unlink, removeSync } from "fs-extra-p"
import { emptyDir, readdir, unlink, removeSync } from "fs-extra-p"
import { Platform } from "out/metadata"
import { cpus, homedir, tmpdir } from "os"
import { cpus, homedir } from "os"
import { TEST_DIR, ELECTRON_VERSION } from "./config"

// we set NODE_PATH in this file, so, we cannot use 'out/awaiter' path here
Expand All @@ -15,36 +14,18 @@ const downloadElectron: (options: any) => Promise<any> = BluebirdPromise.promisi
const packager = require("../../../out/packager")

const rootDir = path.join(__dirname, "..", "..", "..")
const testPackageDir = path.join(tmpdir(), "electron_builder_published")
const testNodeModules = path.join(testPackageDir, "node_modules")

async function main() {
const testDir = TEST_DIR
await BluebirdPromise.all([
deleteOldElectronVersion(),
downloadAllRequiredElectronVersions(),
emptyDir(testDir),
outputFile(path.join(testPackageDir, "package.json"), `{
"private": true,
"version": "1.0.0",
"name": "test",
"dependencies": {
"electron-builder": "file:${path.posix.join(__dirname.replace(/\\/g, "/"), "..", "..")}"
}
}`)
.then(() => copyDependencies())
])

// install from cache - all dependencies are already installed before run test
// https://github.com/npm/npm/issues/2568
await exec(["install", "--cache-min", "999999999", "--production", rootDir])
// prune stale packages
await exec(["prune", "--production"])

process.on("SIGINT", () => {
removeSync(testDir)
})

try {
await runTests()
}
Expand Down Expand Up @@ -106,28 +87,6 @@ function downloadAllRequiredElectronVersions(): Promise<any> {
return BluebirdPromise.all(downloadPromises)
}

// npm is very slow and not reliable - so, just copy and then prune dev dependencies
async function copyDependencies() {
await emptyDir(testNodeModules)
const devDeps = Object.keys((await readJson(path.join(rootDir, "package.json"), "utf-8")).devDependencies)
const filtered = new Set()
/*eslint prefer-const: 0*/
for (let name of devDeps) {
filtered.add(path.join(rootDir, "node_modules", name))
}

filtered.add(path.join(rootDir, "node_modules", ".bin"))

return copy(path.join(rootDir, "node_modules"), testNodeModules, {
filter: it => {
if (it.includes("node_modules" + path.sep + "babel-")) {
return false
}
return !filtered.has(it)
}
})
}

/**
* CIRCLE_NODE_INDEX=2 — test nodejs 4 (on Circle).
*/
Expand Down Expand Up @@ -174,46 +133,12 @@ function runTests(): BluebirdPromise<any> {
return utilSpawn(path.join(rootDir, "node_modules", ".bin", "ava"), args, {
cwd: rootDir,
env: Object.assign({}, process.env, {
NODE_PATH: path.join(testNodeModules, "electron-builder"),
NODE_PATH: rootDir,
SKIP_WIN: skipWin,
CSC_IDENTITY_AUTO_DISCOVERY: "false",
TEST_DIR: TEST_DIR,
}),
shell: process.platform === "win32",
stdio: "inherit"
})
}

function exec(args: Array<string>) {
return new BluebirdPromise((resolve, reject) => {
let command = "npm"
const npmExecPath = process.env.npm_execpath || process.env.NPM_CLI_JS
if (npmExecPath != null) {
args.unshift(npmExecPath)
command = process.env.npm_node_execpath || process.env.NODE_EXE || "node"
}

const effectiveOptions = {
stdio: ["ignore", "ignore", "inherit"],
cwd: testPackageDir,
}
// console.log(`Execute ${command} ${args.join(" ")} (cwd: ${effectiveOptions.cwd})`)
const child = spawn(command, args, effectiveOptions)
child.on("close", (code: number) => {
if (code === 0) {
resolve()
}
else {
try {
console.error(readFileSync(path.join(testPackageDir, "npm-debug.log"), "utf8"))
}
catch (e) {
// ignore
}
reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`))
}
})
child.on("error", (error: Error) => {
reject(new Error(`Failed to start child process: ${command} ${args.join(" ")}` + (error.stack || error)))
})
})
}
15 changes: 15 additions & 0 deletions test/typings/depcheck.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module "depcheck" {
interface DepCheckOptions {
ignoreDirs?: Array<string>
}

interface DepCheckResult {
dependencies: Array<string>
devDependencies: Array<string>
missing: Array<string>

using: { [name: string]: Array<string>; }
}

export default function (directory: string, options: DepCheckOptions, callback: (result: DepCheckResult) => void)
}
Loading

0 comments on commit 3eb2752

Please sign in to comment.