Skip to content

Commit

Permalink
[chore] Replace fs-promise with fs-extra (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and skogsmaskin committed Sep 29, 2017
1 parent 5aec13d commit 0d965b7
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@sanity/generate-help-url": "^0.113.7",
"@sanity/resolver": "^0.113.7",
"chalk": "^1.1.3",
"fs-promise": "^2.0.0",
"fs-extra": "^4.0.2",
"in-publish": "^2.0.0",
"promise-props-recursive": "^1.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/check/src/bin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/* eslint-disable no-console, no-process-env, no-process-exit */
const path = require('path')
const fsp = require('fs-promise')
const fse = require('fs-extra')
const chalk = require('chalk')
const publish = require('in-publish')
const sanityCheck = require('./sanityCheck')
Expand Down Expand Up @@ -39,7 +39,7 @@ if (showVersion) {
process.exit()
}

fsp.readJson(manifestDir)
fse.readJson(manifestDir)
.catch(err => {
console.error(chalk.red(
`${tag} Failed to read "${manifestDir}":\n${err.message}`
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/check/src/sanityCheck.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path')
const fsp = require('fs-promise')
const fse = require('fs-extra')
const promiseProps = require('promise-props-recursive')
const generateHelpUrl = require('@sanity/generate-help-url')
const resolveParts = require('@sanity/resolver').resolveParts
Expand Down Expand Up @@ -56,7 +56,7 @@ function throwOnErrors(results, options) {
function getFolderContents(dirs) {
return promiseProps(dirs.reduce((folders, dir) => {
if (!folders[dir]) {
folders[dir] = fsp.readdir(dir).catch(() => [])
folders[dir] = fse.readdir(dir).catch(() => [])
}
return folders
}, {}))
Expand Down Expand Up @@ -106,7 +106,7 @@ function checkImplementationMsg(impl) {
}

function isFileOrDirectoryWithIndex(impl) {
return fsp.stat(impl.path).then(stats => {
return fse.stat(impl.path).then(stats => {
return stats.isDirectory()
? directoryHasIndex(impl)
: true
Expand All @@ -117,7 +117,7 @@ function isFileOrDirectoryWithIndex(impl) {
}

function directoryHasIndex(impl) {
return fsp.readdir(impl.path).then(dirContent => {
return fse.readdir(impl.path).then(dirContent => {
return includes(dirContent, 'index.js')
? true
: new Error(
Expand Down
3 changes: 2 additions & 1 deletion packages/@sanity/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"debug": "^2.6.3",
"deep-sort-object": "^1.0.1",
"filesize": "^3.5.6",
"fs-promise": "^2.0.0",
"fs-extra": "^4.0.2",
"get-uri": "^2.0.1",
"got": "^6.7.1",
"json-lexer": "^1.1.1",
Expand All @@ -42,6 +42,7 @@
"simple-concat": "^1.0.0",
"split2": "^2.1.1",
"tar-fs": "^1.15.2",
"thenify": "^3.3.0",
"through2": "^2.0.3",
"uglify-js": "^3.0.23"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/core/src/actions/build/buildStaticAssets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsp from 'fs-promise'
import fse from 'fs-extra'
import path from 'path'
import rimTheRaf from 'rimraf'
import thenify from 'thenify'
Expand Down Expand Up @@ -81,7 +81,7 @@ export default async (args, context) => {
bundle.stats = stats

if (flags.profile) {
await fsp.writeFile(
await fse.writeFile(
path.join(workDir, 'build-stats.json'),
JSON.stringify(statistics.toJson('verbose'))
)
Expand All @@ -101,7 +101,7 @@ export default async (args, context) => {
})

// Write index file to output destination
await fsp.writeFile(
await fse.writeFile(
path.join(outputDir, 'index.html'),
`<!doctype html>${ReactDOM.renderToStaticMarkup(doc)}`
)
Expand Down Expand Up @@ -133,7 +133,7 @@ export default async (args, context) => {
}

// Copy static assets (from /static folder) to output dir
await fsp.copy(path.join(workDir, 'static'), path.join(outputDir, 'static'), {overwrite: false})
await fse.copy(path.join(workDir, 'static'), path.join(outputDir, 'static'), {overwrite: false})
} catch (err) {
spin.fail()
throw err
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/core/src/actions/build/compressJavascript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsp from 'fs-promise'
import fse from 'fs-extra'
import {spawn} from 'child_process'
import concat from 'simple-concat'

Expand All @@ -7,10 +7,10 @@ export default inputFile => {
const sourceFile = `${inputFile}.source`

return new Promise(async (resolve, reject) => {
await fsp.rename(inputFile, sourceFile)
await fse.rename(inputFile, sourceFile)

const uglify = spawn(uglifyPath, ['-c', '-m', '--', sourceFile])
uglify.stdout.pipe(fsp.createWriteStream(inputFile))
uglify.stdout.pipe(fse.createWriteStream(inputFile))

let error = ''
concat(uglify.stderr, (err, buf) => {
Expand All @@ -26,7 +26,7 @@ export default inputFile => {
return reject(new Error(error))
}

await fsp.unlink(sourceFile)
await fse.unlink(sourceFile)
return resolve()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import pathExists from 'path-exists'
import fsp from 'fs-promise'
import fse from 'fs-extra'
import resolveTree from '@sanity/resolver'
import normalizePluginName from '../../util/normalizePluginName'
import generateConfigChecksum from '../../util/generateConfigChecksum'
Expand Down Expand Up @@ -39,7 +39,7 @@ export default async function reinitializePluginConfigs(options, flags = {}) {
output.print(`Plugin "${plugin.name}" is missing local configuration file, creating ${prtPath}`)
}

return fsp.copy(srcPath, dstPath).then(() => plugin)
return fse.copy(srcPath, dstPath).then(() => plugin)
}

function warnOnDifferingChecksum(plugin) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@sanity/core/src/actions/deploy/deployAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import zlib from 'zlib'
import fsp from 'fs-promise'
import fse from 'fs-extra'
import tar from 'tar-fs'
import lazyRequire from '@sanity/util/lib/lazyRequire'

Expand Down Expand Up @@ -80,7 +80,7 @@ export default async (args, context) => {

async function checkDir(sourceDir) {
try {
const stats = await fsp.stat(sourceDir)
const stats = await fse.stat(sourceDir)
if (!stats.isDirectory()) {
throw new Error(`Directory ${sourceDir} is not a directory`)
}
Expand All @@ -93,7 +93,7 @@ async function checkDir(sourceDir) {
}

try {
await fsp.stat(path.join(sourceDir, 'index.html'))
await fse.stat(path.join(sourceDir, 'index.html'))
} catch (err) {
const error = err.code === 'ENOENT' ? new Error([
`"${sourceDir}/index.html" does not exist -`,
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/core/src/actions/exec/execScript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const spawn = require('child_process').spawn
const fsp = require('fs-promise')
const fse = require('fs-extra')
const path = require('path')

module.exports = async args => {
Expand All @@ -10,7 +10,7 @@ module.exports = async args => {
throw new Error('SCRIPT must be provided. `sanity exec <script>`')
}

if (!await fsp.exists(scriptPath)) {
if (!await fse.exists(scriptPath)) {
throw new Error(`${scriptPath} does not exist`)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import fsp from 'fs-promise'
import fse from 'fs-extra'
import split from 'split2'
import prettyMs from 'pretty-ms'
import streamDataset from '../../actions/dataset/streamDataset'
Expand Down Expand Up @@ -48,7 +48,7 @@ export default {
streamDataset(client, dataset)
.pipe(split())
.pipe(skipSystemDocuments)
.pipe(outputPath ? fsp.createWriteStream(outputPath) : process.stdout)
.pipe(outputPath ? fse.createWriteStream(outputPath) : process.stdout)
.on('error', err => output.error(err))
.on('close', () => {
if (outputPath) {
Expand All @@ -70,7 +70,7 @@ async function getOutputPath(destination, dataset) {

let dstStats = null
try {
dstStats = await fsp.stat(dstPath)
dstStats = await fse.stat(dstPath)
} catch (err) {
// Do nothing
}
Expand All @@ -84,7 +84,7 @@ async function getOutputPath(destination, dataset) {
? path.dirname(dstPath)
: dstPath

await fsp.mkdirs(createPath)
await fse.mkdirs(createPath)
}

return looksLikeFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import got from 'got'
import padStart from 'lodash/padStart'
import fsp from 'fs-promise'
import fse from 'fs-extra'
import prettyMs from 'pretty-ms'
import linecount from 'linecount/promise'
import debug from '../../debug'
Expand Down Expand Up @@ -39,7 +39,7 @@ export default {
const sourceFile = isUrl ? file : path.resolve(process.cwd(), file)
const inputStream = isUrl
? got.stream(sourceFile)
: fsp.createReadStream(sourceFile)
: fse.createReadStream(sourceFile)
const client = apiClient()

const documentCount = isUrl ? 0 : await linecount(sourceFile)
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/core/src/commands/install/installCommand.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsp from 'fs-promise'
import fse from 'fs-extra'
import path from 'path'
import generateConfigChecksum from '../../util/generateConfigChecksum'
import addPluginToManifest from '@sanity/util/lib/addPluginToManifest'
Expand Down Expand Up @@ -45,18 +45,18 @@ async function copyConfiguration(rootDir, fullName, shortName, output) {
const configPath = path.join(rootDir, 'node_modules', fullName, 'config.dist.json')
const dstPath = path.join(rootDir, 'config', `${shortName}.json`)

if (!fsp.existsSync(configPath)) {
if (!fse.existsSync(configPath)) {
return
}

// Configuration exists, check if user has local configuration already
if (fsp.existsSync(dstPath)) {
if (fse.existsSync(dstPath)) {
const distChecksum = await generateConfigChecksum(configPath)
const sameChecksum = await hasSameChecksum(rootDir, fullName, distChecksum)
warnOnDifferentChecksum(shortName, sameChecksum, output.print)
} else {
// Destination file does not exist, copy
await fsp.copy(configPath, dstPath)
await fse.copy(configPath, dstPath)
const checksum = await generateConfigChecksum(configPath)
await setChecksum(rootDir, fullName, checksum)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsp from 'fs-promise'
import fse from 'fs-extra'
import path from 'path'
import {without} from 'lodash'
import readLocalManifest from '@sanity/util/lib/readLocalManifest'
Expand Down Expand Up @@ -54,15 +54,15 @@ async function removeConfiguration(workDir, fullName, shortName, prompt) {
async function removeFromSanityManifest(workDir, pluginName) {
const manifest = await readLocalManifest(workDir, 'sanity.json')
manifest.plugins = without(manifest.plugins || [], pluginName)
return fsp.writeJson(path.join(workDir, 'sanity.json'), manifest, {spaces: 2})
return fse.writeJson(path.join(workDir, 'sanity.json'), manifest, {spaces: 2})
}

function deleteConfiguration(configPath, userConfirmed) {
if (!userConfirmed) {
return Promise.resolve() // Leave the configuration in place
}

return fsp.unlink(configPath)
return fse.unlink(configPath)
}

function promptOnAlteredConfiguration(plugin, sameChecksum, prompt) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/core/src/util/generateConfigChecksum.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import crypto from 'crypto'
import fsp from 'fs-promise'
import fse from 'fs-extra'
import deepSortObject from 'deep-sort-object'

function generateConfigChecksum(configPath) {
return fsp.readJson(configPath)
return fse.readJson(configPath)
.then(deepSortObject)
.then(generateChecksum)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/core/src/util/pluginChecksumManifest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsp from 'fs-promise'
import fse from 'fs-extra'
import pathExists from 'path-exists'
import path from 'path'
import normalizePluginName from './normalizePluginName'
Expand All @@ -10,21 +10,21 @@ const baseChecksums = {
export function setChecksum(sanityPath, pluginName, checksum) {
return getChecksums(sanityPath).then(checksums => {
checksums[pluginName] = checksum
return fsp.writeJson(getChecksumsPath(sanityPath), checksums, {spaces: 2})
return fse.writeJson(getChecksumsPath(sanityPath), checksums, {spaces: 2})
})
}

export function setChecksums(sanityPath, checksums) {
const sums = Object.assign({}, baseChecksums, checksums)
return fsp.writeJson(getChecksumsPath(sanityPath), sums, {spaces: 2})
return fse.writeJson(getChecksumsPath(sanityPath), sums, {spaces: 2})
}

export function getChecksum(sanityPath, pluginName) {
return getChecksums(sanityPath).then(sums => sums[pluginName])
}

export function getChecksums(sanityPath) {
return fsp.readJson(getChecksumsPath(sanityPath))
return fse.readJson(getChecksumsPath(sanityPath))
.catch(() => baseChecksums)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"dependencies": {
"@sanity/generate-help-url": "^0.113.7",
"@sanity/util": "^0.113.7",
"fs-extra": "^4.0.2",
"lodash": "^4.17.4",
"mz": "^2.4.0",
"path-exists": "^3.0.0",
"promise-props-recursive": "^1.0.0"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@sanity/resolver/src/readManifest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable no-sync, no-process-env */
import fsp from 'mz/fs'
import fse from 'fs-extra'
import path from 'path'
import generateHelpUrl from '@sanity/generate-help-url'
import {reduceConfig} from '@sanity/util'
import validateManifest from './validateManifest'

function readManifestSync(manifestPath, options) {
try {
return parseManifest(fsp.readFileSync(manifestPath), options)
return parseManifest(fse.readFileSync(manifestPath), options)
} catch (err) {
return handleManifestReadError(err, options)
}
Expand Down Expand Up @@ -44,7 +44,7 @@ function readManifest(opts = {}) {
return readManifestSync(manifestPath, options)
}

return fsp.readFile(manifestPath, {encoding: 'utf8'})
return fse.readFile(manifestPath, {encoding: 'utf8'})
.then(raw => parseManifest(raw, options))
.catch(err => handleManifestReadError(err, options))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/sanity-io/util#readme",
"dependencies": {
"fs-promise": "^2.0.0",
"fs-extra": "^4.0.2",
"lodash": "^4.17.4",
"resolve-from": "^4.0.0"
},
Expand Down
Loading

0 comments on commit 0d965b7

Please sign in to comment.