Skip to content

Commit

Permalink
refactor/polish ~ prettier reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Dec 13, 2020
1 parent e4eff74 commit 5680208
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 92 deletions.
10 changes: 5 additions & 5 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# spell-checker:ignore repo

version: "{build} ~ {branch}"
version: '{build} ~ {branch}'

branches:
except:
- gh-pages

environment:
matrix:
- nodejs_version: "12"
- nodejs_version: "10"
- nodejs_version: "8"
- nodejs_version: "6"
- nodejs_version: '12'
- nodejs_version: '10'
- nodejs_version: '8'
- nodejs_version: '6'
# - nodejs_version: "4"

# platform:
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ npm install os-paths
> #### Requirements
>
> NodeJS >= 6.0.0
<!--{blockquote: .--info style="font-size:75%;"}-->

## Usage
Expand All @@ -55,7 +56,7 @@ osPaths.temp();
```js
const osPaths = require('os-paths');
// or ...
const osPaths = require('os-paths')( options );
const osPaths = require('os-paths')(options);
```

The object returned by the module constructor is an `OSPaths` function object, augmented with attached methods. When called directly (eg, `const p = osPaths()`), it returns a newly constructed `OSPaths` object. Since the `OSPaths` object contains no instance state, all constructed objects will be functionally identical.
Expand All @@ -64,8 +65,8 @@ The object returned by the module constructor is an `OSPaths` function object, a

All module methods return simple, platform-compatible, path strings.

The path strings are *not* guaranteed to already exist on the file system. So, the user is responsible for directory construction, if/when needed.
However, since all of these are *standard* OS directories, they should all exist without the need for user intervention.
The path strings are _not_ guaranteed to already exist on the file system. So, the user is responsible for directory construction, if/when needed.
However, since all of these are _standard_ OS directories, they should all exist without the need for user intervention.

If/when necessary, [`make-dir`](https://www.npmjs.com/package/make-dir) or [`mkdirp`](https://www.npmjs.com/package/mkdirp) can be used to create the directories.

Expand Down Expand Up @@ -100,30 +101,39 @@ MIT © [Roy Ivy III](https://github.com/rivy), [Sindre Sorhus](https://sindresor
[node-url]: https://npmjs.org/package/os-paths

<!-- [npm-image]: https://img.shields.io/npm/v/os-paths.svg?style=flat&label=npm&logo=NPM&logoColor=linen -->

[npm-image]: https://img.shields.io/npm/v/os-paths.svg?style=flat
[npm-url]: https://npmjs.org/package/os-paths

<!-- [appveyor-image]: https://ci.appveyor.com/api/projects/status/.../branch/master?svg=true -->

[appveyor-image]: https://img.shields.io/appveyor/ci/rivy/js-os-paths/master.svg?style=flat&logo=AppVeyor&logoColor=deepskyblue
[appveyor-url]: https://ci.appveyor.com/project/rivy/js-os-paths

<!-- [travis-image]: https://travis-ci.org/rivy/js.os-paths.svg?branch=master -->
<!-- [travis-image]: https://img.shields.io/travis/rivy/js.os-paths/master.svg?style=flat&logo=Travis-CI&logoColor=silver -->

[travis-image]: https://img.shields.io/travis/rivy/js.os-paths/master.svg?style=flat&logo=travis
[travis-url]: https://travis-ci.org/rivy/js.os-paths

<!-- [coverage-image]: https://img.shields.io/coveralls/github/rivy/os-paths/master.svg -->
<!-- [coverage-url]: https://coveralls.io/github/rivy/os-paths -->

[coverage-image]: https://img.shields.io/codecov/c/github/rivy/js.os-paths/master.svg
[coverage-url]: https://codecov.io/gh/rivy/js.os-paths
[downloads-image]: http://img.shields.io/npm/dm/os-paths.svg?style=flat
[downloads-url]: https://npmjs.org/package/os-paths
[license-image]: https://img.shields.io/npm/l/os-paths.svg?style=flat
[license-url]: license

<!-- [repository-image]:https://img.shields.io/badge/%E2%9D%A4-darkcyan?style=flat&logo=github -->
<!-- [repository-image]:https://img.shields.io/github/v/tag/rivy/js.os-paths?label=%e2%80%8b&logo=github&logoColor=white&colorA=gray&logoWidth=15 -->
[repository-image]:https://img.shields.io/github/v/tag/rivy/js.os-paths?label=repo&logo=github&logoColor=white
[repository-url]:https://github.com/rivy/js.os-paths

[repository-image]: https://img.shields.io/github/v/tag/rivy/js.os-paths?label=repo&logo=github&logoColor=white
[repository-url]: https://github.com/rivy/js.os-paths

<!-- [style-image]: https://img.shields.io/badge/code_style-standard-darkcyan.svg -->
<!-- [style-url]: https://standardjs.com -->

[style-image]: https://img.shields.io/badge/code_style-XO-darkcyan.svg
[style-url]: https://github.com/xojs/xo
2 changes: 1 addition & 1 deletion eg/show-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (_) {
});
}

process.env.TMPDIR = process.env.TEMP = process.TMP = 'temp'; // eslint-disable-line no-multi-assign
process.env.TMPDIR = process.env.TEMP = process.TMP = 'temp'; // eslint-disable-line no-multi-assign
if (_) {
_.each(osPaths, (value, key) => {
console.log(key, '=', osPaths[key]());
Expand Down
119 changes: 61 additions & 58 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,77 @@ const os = require('os');
const isWinOS = /^win/i.test(process.platform);

const base = () => {
const {env} = process;
const { env } = process;

const object = {};

object.home = os.homedir ?
() => {
return os.homedir();
} :
() => {
let path = env.HOME;
if (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1);
}

return path;
};

object.temp = os.tmpdir ?
() => {
return os.tmpdir();
} :
() => {
let path = env.TMPDIR ||
env.TEMP ||
env.TMP ||
'/tmp';
if (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1);
}

return path;
};
object.home = os.homedir
? () => {
return os.homedir();
}
: () => {
let path = env.HOME;
if (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1);
}

return path;
};

object.temp = os.tmpdir
? () => {
return os.tmpdir();
}
: () => {
let path = env.TMPDIR || env.TEMP || env.TMP || '/tmp';
if (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1);
}

return path;
};

return object;
};

const windows = () => {
const {env} = process;
const { env } = process;

const object = {};

object.home = os.homedir ?
() => {
return os.homedir();
} :
() => {
let path = env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || env.HOME;
if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) {
path = path.slice(0, -1);
}

return path;
};

object.temp = os.tmpdir ?
() => {
return os.tmpdir();
} :
() => {
let path = env.TEMP ||
env.TMP ||
(env.SystemRoot || env.windir) + '\\temp';
if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) {
path = path.slice(0, -1);
}

return path;
};
object.home = os.homedir
? () => {
return os.homedir();
}
: () => {
let path = env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || env.HOME;
if (
path.length > 1 &&
((path.endsWith('\\') && !path.endsWith(':\\')) ||
(path.endsWith('/') && !path.endsWith(':/')))
) {
path = path.slice(0, -1);
}

return path;
};

object.temp = os.tmpdir
? () => {
return os.tmpdir();
}
: () => {
let path = env.TEMP || env.TMP || (env.SystemRoot || env.windir) + '\\temp';
if (
path.length > 1 &&
((path.endsWith('\\') && !path.endsWith(':\\')) ||
(path.endsWith('/') && !path.endsWith(':/')))
) {
path = path.slice(0, -1);
}

return path;
};

return object;
};
Expand All @@ -89,7 +92,7 @@ class _OSPaths {

// Connect to platform-specific API functions by extension
const extension = isWinOS ? windows() : base();
Object.keys(extension).forEach(key => {
Object.keys(extension).forEach((key) => {
this._fn[key] = extension[key];
});

Expand Down
20 changes: 10 additions & 10 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ const module_ = require('../src/lib');

// Integration tests

test('api', t => {
test('api', (t) => {
const paths = module_;
const api = ['home', 'temp'];

t.is(typeof paths, 'function');
t.is(Object.keys(paths).length, api.length);
api.forEach(key => {
api.forEach((key) => {
t.is(typeof paths[key], 'function');
});
});

test('examples are executable without error (JavaScript)', t => {
test('examples are executable without error (JavaScript)', (t) => {
const egDirPath = 'eg';
const extensions = ['.js', '.cjs', '.mjs'];

const files = fs.readdirSync(egDirPath);

files
.filter(file => {
.filter((file) => {
return extensions.includes(path.extname(file));
})
.forEach(file => {
.forEach((file) => {
const command = 'node';
const script = path.join(egDirPath, file);
const args = [script];
const options = {shell: true, encoding: 'utf8'};
const options = { shell: true, encoding: 'utf8' };

t.log({script});
t.log({ script });

const {error, status, stdout} = spawn.sync(command, args, options);
const { error, status, stdout } = spawn.sync(command, args, options);

t.log({error, status, stdout});
t.log({ error, status, stdout });

t.deepEqual({error, status}, {error: null, status: 0});
t.deepEqual({ error, status }, { error: null, status: 0 });
});
});
2 changes: 1 addition & 1 deletion test/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import { expectType } from 'tsd';
import osPaths = require('../src/lib');

expectType<typeof osPaths>(osPaths());
Expand Down
27 changes: 15 additions & 12 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,45 @@ import test from 'ava';

import osPaths from '../src/lib';

test('default', t => {
test('default', (t) => {
const paths = osPaths();

process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
process.env.TMPDIR = process.env.TEMP = process.env.TEMP = 'temp'; // eslint-disable-line no-multi-assign
process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
process.env.TMPDIR = process.env.TEMP = process.env.TEMP = 'temp'; // eslint-disable-line no-multi-assign

for (const key of Object.keys(paths)) {
const value = paths[key];
t.log(key, '=', value, '() =>', value());
const values = [].concat(value()); // # convert value (single value or array) to a flat array
t.true(values.reduce((a, v) => a && v === key, true), true);
t.true(
values.reduce((a, v) => a && v === key, true),
true
);
}
});

test('alternate construction (via function)', t => {
test('alternate construction (via function)', (t) => {
const paths = osPaths();
process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
t.is(paths.home(), 'home');
});

test('no os.homedir/os.tmpdir', t => {
test('no os.homedir/os.tmpdir', (t) => {
os.homedir = null;
os.tmpdir = null;
const paths = osPaths();
process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
process.env.TEMP = process.env.TMP = process.env.TMPDIR = 'temp'; // eslint-disable-line no-multi-assign
process.env.HOME = process.env.USERPROFILE = 'home'; // eslint-disable-line no-multi-assign
process.env.TEMP = process.env.TMP = process.env.TMPDIR = 'temp'; // eslint-disable-line no-multi-assign
t.is(paths.home(), 'home');
t.is(paths.temp(), 'temp');
});

test('no os.homedir/os.tmpdir and trailing slash in source', t => {
test('no os.homedir/os.tmpdir and trailing slash in source', (t) => {
os.homedir = null;
os.tmpdir = null;
const paths = osPaths();
process.env.HOME = process.env.USERPROFILE = 'home/'; // eslint-disable-line no-multi-assign
process.env.TEMP = process.env.TMP = process.env.TMPDIR = 'temp/'; // eslint-disable-line no-multi-assign
process.env.HOME = process.env.USERPROFILE = 'home/'; // eslint-disable-line no-multi-assign
process.env.TEMP = process.env.TMP = process.env.TMPDIR = 'temp/'; // eslint-disable-line no-multi-assign
t.is(paths.home(), 'home');
t.is(paths.temp(), 'temp');
});

0 comments on commit 5680208

Please sign in to comment.