Skip to content

Commit

Permalink
Refactor: Use process.platform across codebase (#689)
Browse files Browse the repository at this point in the history
* Use process.platform across codebase

* Use process.platform in tests
  • Loading branch information
freitagbr authored and nfischer committed Apr 15, 2017
1 parent 80d590e commit e12da9d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
3 changes: 0 additions & 3 deletions src/common.js
Expand Up @@ -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 = [];

Expand Down
11 changes: 7 additions & 4 deletions 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: {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand All @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/find.js
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ln.js
Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/which.js
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -61,7 +62,7 @@ function _which(options, cmd) {

var attempt = path.resolve(pathArray[k], cmd);

if (common.platform === 'win') {
if (isWindows) {
attempt = attempt.toUpperCase();
}

Expand Down
2 changes: 1 addition & 1 deletion test/cp.js
Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions test/test.js
@@ -1,7 +1,6 @@
import test from 'ava';

import shell from '..';
import common from '../src/common';

shell.config.silent = true;

Expand Down Expand Up @@ -91,39 +90,39 @@ 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);
}
});

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);
}
});

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);
}
});

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);
}
});

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);
Expand Down

0 comments on commit e12da9d

Please sign in to comment.