Skip to content
This repository was archived by the owner on Sep 15, 2023. It is now read-only.

Commit 6809dd4

Browse files
maouehbenjtinsley
authored andcommitted
Fix resolution of calling project package.json file (Fixes #532)
It appears that `process.env.PWD` is not consistent across platform as well as across sub-directory invocations. Indeed, even if it is consistent on Mac OS X (and probably Unix at the same time but untested), there is inconsitencies problem on Windows when using a POSIX transformer platform like Cygwin or MSYS2. Indeed, in those cases, `PWD` is not the canonical Windows path but the original POSIX path of the terminal. Furthermore, invoking from a sub-directory also fiddles with the `PWD` adding subdirectory into it. To fix that, it seems that Gulp is providing an environment variable that points to the root directory of the project and which is constant across platforms. See gulpjs/gulp@2bf23ba for the actual INIT_CWD implementation. So, to fix the problem everywhere, added a function `resolveRelativeToProject` that does the correct resolving using `INIT_CWD` environment variable instead of the previously used `PWD`. To make stuff easier to use next time, a `projectPath` function was added that does the resolving of paths relative to the project root directory. All previous usages of `path.(resolve|join)(process.env.PWD, ...)` that were used throughout the website are now using `projectPath(...)` instead. Fixed bin script `blendid/gulpfile.js/index.js` resolution to use `__dirname` instead of `require.resolve`. The `require.resolve` does not work when using `yarn link` to development `blendid` features. Indeed, `require.resolve` does the resolution from the linked project leading to `blendid` not being resolvable (because not present in the linked `node_modules` directory). By using `__dirname`, we achieve the same goal as before but being more robust to linked project for development.
1 parent fea876c commit 6809dd4

33 files changed

+165
-138
lines changed

bin/blendid.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
2-
const path = require('path');
2+
const path = require('path')
33

44
const additionalArgs = require('minimist')(process.argv.slice(2))._
5-
const blendidEntryFile = require.resolve('blendid');
6-
const gulpModulePath = path.dirname(require.resolve('gulp'));
7-
const gulpBinaryFile = path.join(gulpModulePath, '/bin/gulp');
5+
const blendidEntryFile = path.resolve(__dirname, '../gulpfile.js/index.js')
6+
const gulpModulePath = path.dirname(require.resolve('gulp'))
7+
const gulpBinaryFile = path.join(gulpModulePath, '/bin/gulp')
88

99
let args = ['--gulpfile', blendidEntryFile]
1010

extras/tasks/iconFont/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ var generateIconSass = require('./generateIconSass')
77
var handleErrors = require('../../lib/handleErrors')
88
var package = require('../../../package.json')
99
var path = require('path')
10+
var projectPath = require('../../lib/projectPath')
1011
var url = require('url')
1112

12-
var fontPath = path.resolve(process.env.PWD, config.root.dest, config.tasks.iconFont.dest)
13-
var cssPath = path.resolve(process.env.PWD, config.root.dest, config.tasks.css.dest)
13+
var fontPath = projectPath(config.root.dest, config.tasks.iconFont.dest)
14+
var cssPath = projectPath(config.root.dest, config.tasks.css.dest)
1415

1516
var settings = {
1617
name: package.name + ' icons',
17-
src: path.resolve(process.env.PWD, config.root.src, config.tasks.iconFont.src, '*.svg'),
18-
dest: path.resolve(process.env.PWD, config.root.dest, config.tasks.iconFont.dest),
19-
sassDest: path.resolve(process.env.PWD, config.root.src, config.tasks.css.src, config.tasks.iconFont.sassDest),
18+
src: projectPath(config.root.src, config.tasks.iconFont.src, '*.svg'),
19+
dest: projectPath(config.root.dest, config.tasks.iconFont.dest),
20+
sassDest: projectPath(config.root.src, config.tasks.css.src, config.tasks.iconFont.sassDest),
2021
template: path.normalize('./gulpfile.js/tasks/iconFont/template.sass'),
2122
sassOutputName: '_icons.sass',
2223
fontPath: url.resolve('.',path.relative(cssPath, fontPath)),

extras/tasks/server.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
var compress = require('compression')
2-
var config = require('../config')
3-
var express = require('express')
4-
var gulp = require('gulp')
5-
var log = require('fancy-log')
6-
var colors = require('ansi-colors')
7-
var logger = require('morgan')
8-
var open = require('open')
9-
var path = require('path')
1+
var compress = require('compression')
2+
var config = require('../config')
3+
var express = require('express')
4+
var gulp = require('gulp')
5+
var log = require('fancy-log')
6+
var colors = require('ansi-colors')
7+
var logger = require('morgan')
8+
var open = require('open')
9+
var projectPath = require('../lib/projectPath')
10+
1011

1112
var settings = {
12-
root: path.resolve(process.env.PWD, config.root.dest),
13+
root: projectPath(config.root.dest),
1314
port: process.env.PORT || 5000,
1415
logLevel: process.env.NODE_ENV ? 'combined' : 'dev',
1516
staticOptions: {

gulpfile.js/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
automatically required below.
88
*/
99

10-
const path = require('path')
1110
const gulp = require('gulp')
1211
const requireDir = require('require-dir')
1312

14-
// Fallback for windows backs out of node_modules folder to root of project
15-
process.env.PWD = process.env.PWD || path.resolve(process.cwd(), '../../')
16-
1713
// Globally expose config objects
1814
global.PATH_CONFIG = require('./lib/get-path-config')
1915
global.TASK_CONFIG = require('./lib/get-task-config')

gulpfile.js/lib/get-path-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const path = require('path')
1+
const projectPath = require('./projectPath');
22
const fs = require('fs')
33

44
function getPathConfig() {
55

66
if(process.env.BLENDID_CONFIG_PATH) {
7-
return require(path.resolve(process.env.PWD, process.env.BLENDID_CONFIG_PATH, 'path-config.json'))
7+
return require(projectPath(process.env.BLENDID_CONFIG_PATH, 'path-config.json'))
88
}
99

10-
const defaultConfigPath = path.resolve(process.env.PWD, 'config/path-config.json')
10+
const defaultConfigPath = projectPath('config/path-config.json')
1111

1212
if (fs.existsSync(defaultConfigPath)) {
1313
return require(defaultConfigPath)

gulpfile.js/lib/get-task-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const path = require('path')
21
const fs = require('fs')
2+
const projectPath = require('./projectPath')
33
const taskDefaults = require('./task-defaults')
44
const mergeWith = require('lodash/mergeWith')
55

66
function getTaskConfig () {
77

88
if(process.env.BLENDID_CONFIG_PATH) {
9-
return require(path.resolve(process.env.PWD, process.env.BLENDID_CONFIG_PATH, 'task-config.js'))
9+
return require(projectPath(process.env.BLENDID_CONFIG_PATH, 'task-config.js'))
1010
}
1111

12-
const defaultConfigPath = path.resolve(process.env.PWD, 'config/task-config.js')
12+
const defaultConfigPath = projectPath('config/task-config.js')
1313

1414
if (fs.existsSync(defaultConfigPath)) {
1515
return require(defaultConfigPath)

gulpfile.js/lib/projectPath.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require('path')
2+
3+
/**
4+
* A function that can be used to resolve a path relatively to the
5+
* project directory.
6+
*
7+
* We often want to resolve paths relatively to the project root
8+
* directory. To do that, we use the `INIT_CWD` environment variable
9+
* provided by Gulp. This variable always resolves to the project
10+
* root directory so we use it as the seed path and then add the
11+
* remaining arguments passed and resolving everything using the
12+
* `path.resolve` function.
13+
*
14+
* The returned path is a fully resolved absolute path relative to
15+
* the project root directory.
16+
*/
17+
module.exports = function projectPath(...paths) {
18+
return path.resolve(process.env.INIT_CWD, ...paths);
19+
}

gulpfile.js/lib/task-defaults.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const os = require('os')
2-
const path = require('path')
3-
const pkg = require(path.resolve(process.env.PWD, 'package.json'))
1+
const os = require('os')
2+
const path = require('path')
3+
const projectPath = require('./projectPath')
4+
const pkg = require(projectPath('package.json'))
45

56
module.exports = {
67
javascripts: {

gulpfile.js/lib/webpack-multi-config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!TASK_CONFIG.javascripts) {
66

77
const path = require('path')
88
const pathToUrl = require('./pathToUrl')
9+
const projectPath = require('./projectPath')
910
const webpack = require('webpack')
1011
const webpackManifest = require('./webpackManifest')
1112
const querystring = require('querystring')
@@ -14,8 +15,8 @@ module.exports = function (env) {
1415

1516
process.env['BABEL_ENV'] = process.env['BABEL_ENV'] || process.env['NODE_ENV'] || env
1617

17-
const jsSrc = path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.javascripts.src)
18-
const jsDest = path.resolve(process.env.PWD, PATH_CONFIG.dest, PATH_CONFIG.javascripts.dest)
18+
const jsSrc = projectPath(PATH_CONFIG.src, PATH_CONFIG.javascripts.src)
19+
const jsDest = projectPath(PATH_CONFIG.dest, PATH_CONFIG.javascripts.dest)
1920
const publicPath = pathToUrl(TASK_CONFIG.javascripts.publicPath || PATH_CONFIG.javascripts.dest, '/')
2021
const rev = TASK_CONFIG.production.rev && env === 'production'
2122

@@ -39,7 +40,7 @@ module.exports = function (env) {
3940
resolve: {
4041
extensions: extensions,
4142
alias: TASK_CONFIG.javascripts.alias,
42-
modules: [jsSrc, path.resolve(process.env.PWD, 'node_modules')],
43+
modules: [jsSrc, projectPath('node_modules')],
4344
},
4445
module: {
4546
rules: [ TASK_CONFIG.javascripts.babelLoader ]

gulpfile.js/lib/webpackManifest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const path = require('path')
2-
const fs = require('fs')
1+
const path = require('path')
2+
const projectPath = require('./projectPath')
3+
const fs = require('fs')
34

45
module.exports = function(jsDest, dest, filename) {
56
filename = filename || 'rev-manifest.json'
@@ -18,7 +19,7 @@ module.exports = function(jsDest, dest, filename) {
1819
}
1920

2021
fs.writeFileSync(
21-
path.resolve(process.env.PWD, dest, filename),
22+
projectPath(dest, filename),
2223
JSON.stringify(manifest)
2324
)
2425
})

0 commit comments

Comments
 (0)