From e12da9d888c03b0b7cc6c75bcf9a3251fa2322fd Mon Sep 17 00:00:00 2001 From: Brandon Freitag Date: Fri, 14 Apr 2017 23:04:21 -0700 Subject: [PATCH] Refactor: Use process.platform across codebase (#689) * Use process.platform across codebase * Use process.platform in tests --- src/common.js | 3 --- src/cp.js | 11 +++++++---- src/find.js | 2 +- src/ln.js | 2 +- src/which.js | 5 +++-- test/cp.js | 2 +- test/test.js | 11 +++++------ 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/common.js b/src/common.js index f5197d8e..e0d343b8 100644 --- a/src/common.js +++ b/src/common.js @@ -66,9 +66,6 @@ exports.state = state; delete process.env.OLDPWD; // initially, there's no previous directory -var platform = os.type().match(/^Win/) ? 'win' : 'unix'; -exports.platform = platform; - // This is populated by calls to commonl.wrap() var pipeMethods = []; diff --git a/src/cp.js b/src/cp.js index 04c4e57e..52af77be 100644 --- a/src/cp.js +++ b/src/cp.js @@ -1,7 +1,6 @@ var fs = require('fs'); var path = require('path'); var common = require('./common'); -var os = require('os'); common.register('cp', _cp, { cmdOptions: { @@ -24,6 +23,8 @@ function copyFileSync(srcFile, destFile, options) { common.error('copyFileSync: no such file or directory: ' + srcFile); } + var isWindows = process.platform === 'win32'; + // Check the mtimes of the files if the '-u' flag is provided try { if (options.update && fs.statSync(srcFile).mtime < fs.statSync(destFile).mtime) { @@ -42,7 +43,7 @@ function copyFileSync(srcFile, destFile, options) { } var symlinkFull = fs.readlinkSync(srcFile); - fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); + fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); } else { var BUF_LENGTH = 64 * 1024; var buf = new Buffer(BUF_LENGTH); @@ -93,6 +94,8 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { if (currentDepth >= common.config.maxdepth) return; currentDepth++; + var isWindows = process.platform === 'win32'; + // Create the directory where all our junk is moving to; read the mode of the // source directory and mirror it try { @@ -116,7 +119,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { // Cycle link found. console.error('Cycle link found.'); symlinkFull = fs.readlinkSync(srcFile); - fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); + fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); continue; } } @@ -131,7 +134,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { } catch (e) { // it doesn't exist, so no work needs to be done } - fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); + fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); } else if (srcFileStat.isSymbolicLink() && opts.followsymlink) { srcFileStat = fs.statSync(srcFile); if (srcFileStat.isDirectory()) { diff --git a/src/find.js b/src/find.js index 625aa297..76a16c4e 100644 --- a/src/find.js +++ b/src/find.js @@ -30,7 +30,7 @@ function _find(options, paths) { var list = []; function pushFile(file) { - if (common.platform === 'win') { + if (process.platform === 'win32') { file = file.replace(/\\/g, '/'); } list.push(file); diff --git a/src/ln.js b/src/ln.js index 7393d9fc..9b8beb9e 100644 --- a/src/ln.js +++ b/src/ln.js @@ -43,7 +43,7 @@ function _ln(options, source, dest) { } if (options.symlink) { - var isWindows = common.platform === 'win'; + var isWindows = process.platform === 'win32'; var linkType = isWindows ? 'file' : null; var resolvedSourcePath = isAbsolute ? sourcePath : path.resolve(process.cwd(), path.dirname(dest), source); if (!fs.existsSync(resolvedSourcePath)) { diff --git a/src/which.js b/src/which.js index 03db57bc..a5f9e15e 100644 --- a/src/which.js +++ b/src/which.js @@ -37,6 +37,7 @@ function checkPath(pathName) { function _which(options, cmd) { if (!cmd) common.error('must specify command'); + var isWindows = process.platform === 'win32'; var pathEnv = process.env.path || process.env.Path || process.env.PATH; var pathArray = splitPath(pathEnv); @@ -47,7 +48,7 @@ function _which(options, cmd) { // Assume that there are no extensions to append to queries (this is the // case for unix) var pathExtArray = ['']; - if (common.platform === 'win') { + if (isWindows) { // In case the PATHEXT variable is somehow not set (e.g. // child_process.spawn with an empty environment), use the XP default. var pathExtEnv = process.env.PATHEXT || XP_DEFAULT_PATHEXT; @@ -61,7 +62,7 @@ function _which(options, cmd) { var attempt = path.resolve(pathArray[k], cmd); - if (common.platform === 'win') { + if (isWindows) { attempt = attempt.toUpperCase(); } diff --git a/test/cp.js b/test/cp.js index 76525f09..94f0e23a 100644 --- a/test/cp.js +++ b/test/cp.js @@ -405,7 +405,7 @@ test('recursive, with trailing slash, does the exact same', t => { test( 'On Windows, permission bits are quite different so skip those tests for now', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { // preserve mode bits const execBit = parseInt('001', 8); t.is(fs.statSync('resources/cp-mode-bits/executable').mode & execBit, execBit); diff --git a/test/test.js b/test/test.js index 498bbea6..39f25795 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,6 @@ import test from 'ava'; import shell from '..'; -import common from '../src/common'; shell.config.silent = true; @@ -91,7 +90,7 @@ test('test command is not globbed', t => { // TODO(nate): figure out a way to test links on Windows test('-d option fails for a link', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { const result = shell.test('-d', 'resources/link'); t.falsy(shell.error()); t.falsy(result); @@ -99,7 +98,7 @@ test('-d option fails for a link', t => { }); test('-f option succeeds for a link', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { const result = shell.test('-f', 'resources/link'); t.falsy(shell.error()); t.truthy(result); @@ -107,7 +106,7 @@ test('-f option succeeds for a link', t => { }); test('-L option succeeds for a symlink', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { const result = shell.test('-L', 'resources/link'); t.falsy(shell.error()); t.truthy(result); @@ -115,7 +114,7 @@ test('-L option succeeds for a symlink', t => { }); test('-L option works for broken symlinks', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { const result = shell.test('-L', 'resources/badlink'); t.falsy(shell.error()); t.truthy(result); @@ -123,7 +122,7 @@ test('-L option works for broken symlinks', t => { }); test('-L option fails for missing files', t => { - if (common.platform !== 'win') { + if (process.platform !== 'win32') { const result = shell.test('-L', 'resources/404'); t.falsy(shell.error()); t.falsy(result);