How to use path

Comprehensive path code examples:

How to use path.slice:

20
21
22
23
24
25
26
27
28
29
30
 * @param {string} path 
 * @returns 
 */
const relative2Doc = (path) => {
    const base = docBasePath(path);
    return base ? path.slice(base.length) : path;
}


const ensureTrailingSlash = (path) => {
    if (typeof path !== 'string') {

How to use path.existsSync:

12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
 */


var exec = require('child_process').exec
  , fs = require('fs')
  , path = require('path')
  , exists = fs.existsSync || path.existsSync
  , os = require('os')
  , quote = JSON.stringify
  , cmd;

How to use path.dirname:

20
21
22
23
24
25
26
27
28
29
30
const specFilePath = path.resolve(
  path.join(__dirname, '../../../kolibri/core/assets/src/core-app/apiSpec.js')
);


function specModule(filePath) {
  const rootPath = path.dirname(filePath);
  function newPath(p1) {
    if (p1.startsWith('.')) {
      return path.join(rootPath, p1);
    } else {

How to use path.relative:

46
47
48
49
50
51
52
53
54
55
});

describe('module.children', function() {
  it('should have css dependencies parsed', async function() {
    const mod = porter.packet.files['home.jsx'];
    assert.deepEqual(mod.children.map(child => path.relative(root, child.fpath)), [
      'node_modules/react-dom/index.js',
      'node_modules/react/index.js',
      'app/web/home_dep.js',
      'app/web/utils/index.js',

How to use path.posix:

22
23
24
25
26
27
28
29
30
31
// these devServer options should be customized in /config/index.js
devServer: {
  clientLogLevel: 'warning',
  historyApiFallback: {
    rewrites: [
      { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
    ],
  },
  hot: true,
  contentBase: false, // since we use CopyWebpackPlugin.

How to use path.extname:

247
248
249
250
251
252
253
254
255
256
const destinationFile = path.join(
  destinationFolder,
  extraPath,
  prefix +
    (destinationFileBase
      ? destinationFileBase + path.extname(sourceFile)
      : path.basename(sourceFile))
);
// Copy from the source to the destination.
const sourceContents = fs.readFileSync(sourceFile, { encoding: 'utf-8' });

How to use path.basename:

248
249
250
251
252
253
254
255
256
  destinationFolder,
  extraPath,
  prefix +
    (destinationFileBase
      ? destinationFileBase + path.extname(sourceFile)
      : path.basename(sourceFile))
);
// Copy from the source to the destination.
const sourceContents = fs.readFileSync(sourceFile, { encoding: 'utf-8' });

How to use path.resolve:

113
114
115
116
117
118
119
120
121
122
123
124
}


let baseAliases = {};


const baseAliasSourcePaths = {
  kolibri_module: path.resolve(__dirname, '../../../kolibri/core/assets/src/kolibri_module'),
  kolibri_app: path.resolve(__dirname, '../../../kolibri/core/assets/src/kolibri_app'),
  content_renderer_module: path.resolve(
    __dirname,
    '../../../kolibri/core/assets/src/content_renderer_module'

How to use path.join:

23
24
25
26
27
28
29
30
31
32
33


function specModule(filePath) {
  const rootPath = path.dirname(filePath);
  function newPath(p1) {
    if (p1.startsWith('.')) {
      return path.join(rootPath, p1);
    } else {
      return p1;
    }
  }