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

Commit

Permalink
Fix resolution of calling project package.json file (Fixes #532)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maoueh authored and benjtinsley committed Jan 24, 2018
1 parent fea876c commit 6809dd4
Show file tree
Hide file tree
Showing 33 changed files with 165 additions and 138 deletions.
8 changes: 4 additions & 4 deletions bin/blendid.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
const path = require('path');
const path = require('path')

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

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

Expand Down
11 changes: 6 additions & 5 deletions extras/tasks/iconFont/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ var generateIconSass = require('./generateIconSass')
var handleErrors = require('../../lib/handleErrors')
var package = require('../../../package.json')
var path = require('path')
var projectPath = require('../../lib/projectPath')
var url = require('url')

var fontPath = path.resolve(process.env.PWD, config.root.dest, config.tasks.iconFont.dest)
var cssPath = path.resolve(process.env.PWD, config.root.dest, config.tasks.css.dest)
var fontPath = projectPath(config.root.dest, config.tasks.iconFont.dest)
var cssPath = projectPath(config.root.dest, config.tasks.css.dest)

var settings = {
name: package.name + ' icons',
src: path.resolve(process.env.PWD, config.root.src, config.tasks.iconFont.src, '*.svg'),
dest: path.resolve(process.env.PWD, config.root.dest, config.tasks.iconFont.dest),
sassDest: path.resolve(process.env.PWD, config.root.src, config.tasks.css.src, config.tasks.iconFont.sassDest),
src: projectPath(config.root.src, config.tasks.iconFont.src, '*.svg'),
dest: projectPath(config.root.dest, config.tasks.iconFont.dest),
sassDest: projectPath(config.root.src, config.tasks.css.src, config.tasks.iconFont.sassDest),
template: path.normalize('./gulpfile.js/tasks/iconFont/template.sass'),
sassOutputName: '_icons.sass',
fontPath: url.resolve('.',path.relative(cssPath, fontPath)),
Expand Down
21 changes: 11 additions & 10 deletions extras/tasks/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var compress = require('compression')
var config = require('../config')
var express = require('express')
var gulp = require('gulp')
var log = require('fancy-log')
var colors = require('ansi-colors')
var logger = require('morgan')
var open = require('open')
var path = require('path')
var compress = require('compression')
var config = require('../config')
var express = require('express')
var gulp = require('gulp')
var log = require('fancy-log')
var colors = require('ansi-colors')
var logger = require('morgan')
var open = require('open')
var projectPath = require('../lib/projectPath')


var settings = {
root: path.resolve(process.env.PWD, config.root.dest),
root: projectPath(config.root.dest),
port: process.env.PORT || 5000,
logLevel: process.env.NODE_ENV ? 'combined' : 'dev',
staticOptions: {
Expand Down
4 changes: 0 additions & 4 deletions gulpfile.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
automatically required below.
*/

const path = require('path')
const gulp = require('gulp')
const requireDir = require('require-dir')

// Fallback for windows backs out of node_modules folder to root of project
process.env.PWD = process.env.PWD || path.resolve(process.cwd(), '../../')

// Globally expose config objects
global.PATH_CONFIG = require('./lib/get-path-config')
global.TASK_CONFIG = require('./lib/get-task-config')
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js/lib/get-path-config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const path = require('path')
const projectPath = require('./projectPath');
const fs = require('fs')

function getPathConfig() {

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

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

if (fs.existsSync(defaultConfigPath)) {
return require(defaultConfigPath)
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js/lib/get-task-config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const path = require('path')
const fs = require('fs')
const projectPath = require('./projectPath')
const taskDefaults = require('./task-defaults')
const mergeWith = require('lodash/mergeWith')

function getTaskConfig () {

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

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

if (fs.existsSync(defaultConfigPath)) {
return require(defaultConfigPath)
Expand Down
19 changes: 19 additions & 0 deletions gulpfile.js/lib/projectPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var path = require('path')

/**
* A function that can be used to resolve a path relatively to the
* project directory.
*
* We often want to resolve paths relatively to the project root
* directory. To do that, we use the `INIT_CWD` environment variable
* provided by Gulp. This variable always resolves to the project
* root directory so we use it as the seed path and then add the
* remaining arguments passed and resolving everything using the
* `path.resolve` function.
*
* The returned path is a fully resolved absolute path relative to
* the project root directory.
*/
module.exports = function projectPath(...paths) {
return path.resolve(process.env.INIT_CWD, ...paths);
}
7 changes: 4 additions & 3 deletions gulpfile.js/lib/task-defaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const os = require('os')
const path = require('path')
const pkg = require(path.resolve(process.env.PWD, 'package.json'))
const os = require('os')
const path = require('path')
const projectPath = require('./projectPath')
const pkg = require(projectPath('package.json'))

module.exports = {
javascripts: {
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js/lib/webpack-multi-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if (!TASK_CONFIG.javascripts) {

const path = require('path')
const pathToUrl = require('./pathToUrl')
const projectPath = require('./projectPath')
const webpack = require('webpack')
const webpackManifest = require('./webpackManifest')
const querystring = require('querystring')
Expand All @@ -14,8 +15,8 @@ module.exports = function (env) {

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

const jsSrc = path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.javascripts.src)
const jsDest = path.resolve(process.env.PWD, PATH_CONFIG.dest, PATH_CONFIG.javascripts.dest)
const jsSrc = projectPath(PATH_CONFIG.src, PATH_CONFIG.javascripts.src)
const jsDest = projectPath(PATH_CONFIG.dest, PATH_CONFIG.javascripts.dest)
const publicPath = pathToUrl(TASK_CONFIG.javascripts.publicPath || PATH_CONFIG.javascripts.dest, '/')
const rev = TASK_CONFIG.production.rev && env === 'production'

Expand All @@ -39,7 +40,7 @@ module.exports = function (env) {
resolve: {
extensions: extensions,
alias: TASK_CONFIG.javascripts.alias,
modules: [jsSrc, path.resolve(process.env.PWD, 'node_modules')],
modules: [jsSrc, projectPath('node_modules')],
},
module: {
rules: [ TASK_CONFIG.javascripts.babelLoader ]
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js/lib/webpackManifest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path')
const fs = require('fs')
const path = require('path')
const projectPath = require('./projectPath')
const fs = require('fs')

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

fs.writeFileSync(
path.resolve(process.env.PWD, dest, filename),
projectPath(dest, filename),
JSON.stringify(manifest)
)
})
Expand Down
18 changes: 9 additions & 9 deletions gulpfile.js/tasks/browserSync.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
if(global.production) return

var browserSync = require('browser-sync')
var gulp = require('gulp')
var webpack = require('webpack')
var browserSync = require('browser-sync')
var gulp = require('gulp')
var webpack = require('webpack')
var webpackMultiConfig = require('../lib/webpack-multi-config')
var pathToUrl = require('../lib/pathToUrl')
var path = require('path')
var pathToUrl = require('../lib/pathToUrl')
var projectPath = require('../lib/projectPath')

var browserSyncTask = function() {

Expand All @@ -19,15 +19,15 @@ var browserSyncTask = function() {
}
}

// Resolve path from PWD
// Resolve path from project
if(TASK_CONFIG.browserSync.server && TASK_CONFIG.browserSync.server.baseDir) {
TASK_CONFIG.browserSync.server.baseDir = path.resolve(process.env.PWD, TASK_CONFIG.browserSync.server.baseDir)
TASK_CONFIG.browserSync.server.baseDir = projectPath(TASK_CONFIG.browserSync.server.baseDir)
}

// Resolve files from PWD
// Resolve files from project
if(TASK_CONFIG.browserSync.files) {
TASK_CONFIG.browserSync.files = TASK_CONFIG.browserSync.files.map(function(glob) {
return path.resolve(process.env.PWD, glob)
return projectPath(glob)
})
}

Expand Down
8 changes: 4 additions & 4 deletions gulpfile.js/tasks/clean.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const gulp = require('gulp')
const del = require('del')
const path = require('path')
const gulp = require('gulp')
const del = require('del')
const projectPath = require('../lib/projectPath')

const cleanTask = function (cb) {
var patterns = TASK_CONFIG.clean && TASK_CONFIG.clean.patterns ?
TASK_CONFIG.clean.patterns :
path.resolve(process.env.PWD, PATH_CONFIG.dest)
projectPath(PATH_CONFIG.dest)

return del(patterns, { force: true })
}
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js/tasks/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ if(!TASK_CONFIG.fonts) return
var browserSync = require('browser-sync')
var changed = require('gulp-changed')
var gulp = require('gulp')
var path = require('path')
var projectPath = require('../lib/projectPath')


var fontsTask = function() {

var paths = {
src: path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.fonts.src, '**/*.{' + TASK_CONFIG.fonts.extensions + '}'),
dest: path.resolve(process.env.PWD, PATH_CONFIG.dest, PATH_CONFIG.fonts.dest)
src: projectPath(PATH_CONFIG.src, PATH_CONFIG.fonts.src, '**/*.{' + TASK_CONFIG.fonts.extensions + '}'),
dest: projectPath(PATH_CONFIG.dest, PATH_CONFIG.fonts.dest)
}

return gulp.src([paths.src, '*!README.md'])
Expand Down
12 changes: 6 additions & 6 deletions gulpfile.js/tasks/gh-pages.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const ghPages = require('gulp-gh-pages')
const gulp = require('gulp')
const os = require('os')
const path = require('path')
const ghPages = require('gulp-gh-pages')
const gulp = require('gulp')
const os = require('os')
const projectPath = require('../lib/projectPath')

const ghPagesTask = function() {
const pkg = require(path.resolve(process.env.PWD, 'package.json'))
const pkg = require(projectPath('package.json'))

const settings = {
src: path.resolve(process.env.PWD, PATH_CONFIG.finalDest, '**/*')
src: projectPath(PATH_CONFIG.finalDest, '**/*')
}

return gulp.src(settings.src)
Expand Down
12 changes: 6 additions & 6 deletions gulpfile.js/tasks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ const data = require('gulp-data')
const gulp = require('gulp')
const gulpif = require('gulp-if')
const handleErrors = require('../lib/handleErrors')
const projectPath = require('../lib/projectPath')
const htmlmin = require('gulp-htmlmin')
const path = require('path')
const nunjucksRender = require('gulp-nunjucks-render')
const fs = require('fs')

const htmlTask = function() {

const exclude = '!' + path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.html.src, '**/{' + TASK_CONFIG.html.excludeFolders.join(',') + '}/**')
const exclude = '!' + projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src, '**/{' + TASK_CONFIG.html.excludeFolders.join(',') + '}/**')

const paths = {
src: [path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.html.src, '**/*.{' + TASK_CONFIG.html.extensions + '}'), exclude],
dest: path.resolve(process.env.PWD, PATH_CONFIG.dest, PATH_CONFIG.html.dest),
src: [projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src, '**/*.{' + TASK_CONFIG.html.extensions + '}'), exclude],
dest: projectPath(PATH_CONFIG.dest, PATH_CONFIG.html.dest),
}

const dataFunction = TASK_CONFIG.html.dataFunction || function(file) {
const dataPath = path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.html.src, TASK_CONFIG.html.dataFile)
const dataPath = projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src, TASK_CONFIG.html.dataFile)
return JSON.parse(fs.readFileSync(dataPath, 'utf8'))
}

const nunjucksRenderPath = [ path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.html.src) ]
const nunjucksRenderPath = [ projectPath(PATH_CONFIG.src, PATH_CONFIG.html.src) ]
TASK_CONFIG.html.nunjucksRender.path = TASK_CONFIG.html.nunjucksRender.path || nunjucksRenderPath

return gulp.src(paths.src)
Expand Down
11 changes: 6 additions & 5 deletions gulpfile.js/tasks/http2-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ const gulp = require('gulp')
const log = require('fancy-log')
const colors = require('ansi-colors')
const mergeStream = require('merge-stream')
const path = require('path')
const del = require('del')
const del = require('del')
const projectPath = require('../lib/projectPath')


gulp.task('http2-upgrade', function() {
del([path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.stylesheets.src)], { force: true })
del([projectPath(PATH_CONFIG.src, PATH_CONFIG.stylesheets.src)], { force: true })
log(colors.green('Cleaned stylesheets directory'))

const configStream = gulp.src('../extras/http2/**/*')
.pipe(gulp.dest(process.env.PWD))
.pipe(gulp.dest(projectPath()))

const srcStream = gulp.src(['../src/stylesheets', '../src/javascripts', '../src/html'])
.pipe(gulp.dest(path.join(process.env.PWD, PATH_CONFIG.src)))
.pipe(gulp.dest(projectPath(PATH_CONFIG.src)))

log(colors.green('Created HTTP/2 ready stylesheets directory'))
log(colors.green('Added some HTTP/2 helpers to the html directory'))
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js/tasks/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ if(!TASK_CONFIG.images) return
var browserSync = require('browser-sync')
var changed = require('gulp-changed')
var gulp = require('gulp')
var path = require('path')
var projectPath = require('../lib/projectPath')

var imagesTask = function() {

var paths = {
src: path.resolve(process.env.PWD, PATH_CONFIG.src, PATH_CONFIG.images.src, '**/*.{' + TASK_CONFIG.images.extensions + '}'),
dest: path.resolve(process.env.PWD, PATH_CONFIG.dest, PATH_CONFIG.images.dest)
src: projectPath(PATH_CONFIG.src, PATH_CONFIG.images.src, '**/*.{' + TASK_CONFIG.images.extensions + '}'),
dest: projectPath(PATH_CONFIG.dest, PATH_CONFIG.images.dest)
}

return gulp.src([paths.src, , '*!README.md'])
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js/tasks/init-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var gulp = require('gulp')
var log = require('fancy-log')
var colors = require('ansi-colors')
var path = require('path')
var projectPath = require('../lib/projectPath')
var merge = require('merge-stream')

gulp.task('init-config', function() {
var configStream = gulp.src(['./path-config.json', './task-config.js'])
.pipe(gulp.dest(path.join(process.env.PWD, 'config')))
.pipe(gulp.dest(projectPath('config')))

log(colors.green('Adding default path-config.json and task-config.js files to ./config/'))

Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js/tasks/init-craft.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const gulp = require('gulp')
const log = require('fancy-log')
const colors = require('ansi-colors')
const mergeStream = require('merge-stream')
const path = require('path')
const projectPath = require('../lib/projectPath')

gulp.task('init-craft', function() {
const configStream = gulp.src(['../extras/craft/**/*', '*!ASSET-README.md'])
.pipe(gulp.dest(process.env.PWD))
.pipe(gulp.dest(projectPath()))

const srcStream = gulp.src(['../src/**/*', '*.gitkeep', '!../src/html{,/**}', '!../src/static{,/**}'])
.pipe(gulp.dest(path.join(process.env.PWD, PATH_CONFIG.src)))
.pipe(gulp.dest(projectPath(PATH_CONFIG.src)))


log(colors.green('Added gulpRev plugin to craft/plugins/gulprev!'))
Expand Down
Loading

0 comments on commit 6809dd4

Please sign in to comment.