@cdk8s/projen-common 0.0.237 → 0.0.239

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/.jsii +166 -70
  2. package/.projenrc.ts +17 -0
  3. package/API.md +6745 -54
  4. package/git-hooks/prepare-commit-msg +0 -0
  5. package/git-hooks/setup.sh +0 -0
  6. package/lib/index.d.ts +1 -37
  7. package/lib/index.js +12 -86
  8. package/lib/projects/index.d.ts +2 -0
  9. package/lib/projects/index.js +7 -0
  10. package/lib/projects/jsii.d.ts +48 -0
  11. package/lib/projects/jsii.js +71 -0
  12. package/lib/projects/typescript.d.ts +34 -0
  13. package/lib/projects/typescript.js +89 -0
  14. package/node_modules/codemaker/LICENSE +202 -0
  15. package/node_modules/codemaker/NOTICE +2 -0
  16. package/node_modules/codemaker/README.md +120 -0
  17. package/node_modules/codemaker/jest.config.mjs +3 -0
  18. package/node_modules/codemaker/lib/case-utils.d.ts +4 -0
  19. package/node_modules/codemaker/lib/case-utils.js +29 -0
  20. package/node_modules/codemaker/lib/codemaker.d.ts +107 -0
  21. package/node_modules/codemaker/lib/codemaker.js +164 -0
  22. package/node_modules/codemaker/lib/filebuff.d.ts +11 -0
  23. package/node_modules/codemaker/lib/filebuff.js +26 -0
  24. package/node_modules/codemaker/lib/index.d.ts +3 -0
  25. package/node_modules/codemaker/lib/index.js +19 -0
  26. package/node_modules/codemaker/node_modules/camelcase/index.d.ts +103 -0
  27. package/node_modules/codemaker/node_modules/camelcase/index.js +113 -0
  28. package/node_modules/codemaker/node_modules/camelcase/license +9 -0
  29. package/node_modules/codemaker/node_modules/camelcase/package.json +44 -0
  30. package/node_modules/codemaker/node_modules/camelcase/readme.md +144 -0
  31. package/node_modules/codemaker/package.json +41 -0
  32. package/node_modules/decamelize/index.d.ts +59 -0
  33. package/node_modules/decamelize/index.js +65 -0
  34. package/node_modules/decamelize/license +9 -0
  35. package/node_modules/decamelize/package.json +40 -0
  36. package/node_modules/decamelize/readme.md +92 -0
  37. package/node_modules/fs-extra/LICENSE +15 -0
  38. package/node_modules/fs-extra/README.md +262 -0
  39. package/node_modules/fs-extra/lib/copy/copy-sync.js +169 -0
  40. package/node_modules/fs-extra/lib/copy/copy.js +235 -0
  41. package/node_modules/fs-extra/lib/copy/index.js +7 -0
  42. package/node_modules/fs-extra/lib/empty/index.js +39 -0
  43. package/node_modules/fs-extra/lib/ensure/file.js +69 -0
  44. package/node_modules/fs-extra/lib/ensure/index.js +23 -0
  45. package/node_modules/fs-extra/lib/ensure/link.js +64 -0
  46. package/node_modules/fs-extra/lib/ensure/symlink-paths.js +99 -0
  47. package/node_modules/fs-extra/lib/ensure/symlink-type.js +31 -0
  48. package/node_modules/fs-extra/lib/ensure/symlink.js +82 -0
  49. package/node_modules/fs-extra/lib/fs/index.js +128 -0
  50. package/node_modules/fs-extra/lib/index.js +16 -0
  51. package/node_modules/fs-extra/lib/json/index.js +16 -0
  52. package/node_modules/fs-extra/lib/json/jsonfile.js +11 -0
  53. package/node_modules/fs-extra/lib/json/output-json-sync.js +12 -0
  54. package/node_modules/fs-extra/lib/json/output-json.js +12 -0
  55. package/node_modules/fs-extra/lib/mkdirs/index.js +14 -0
  56. package/node_modules/fs-extra/lib/mkdirs/make-dir.js +27 -0
  57. package/node_modules/fs-extra/lib/mkdirs/utils.js +21 -0
  58. package/node_modules/fs-extra/lib/move/index.js +7 -0
  59. package/node_modules/fs-extra/lib/move/move-sync.js +54 -0
  60. package/node_modules/fs-extra/lib/move/move.js +75 -0
  61. package/node_modules/fs-extra/lib/output-file/index.js +40 -0
  62. package/node_modules/fs-extra/lib/path-exists/index.js +12 -0
  63. package/node_modules/fs-extra/lib/remove/index.js +22 -0
  64. package/node_modules/fs-extra/lib/remove/rimraf.js +302 -0
  65. package/node_modules/fs-extra/lib/util/stat.js +154 -0
  66. package/node_modules/fs-extra/lib/util/utimes.js +26 -0
  67. package/node_modules/fs-extra/package.json +67 -0
  68. package/node_modules/graceful-fs/LICENSE +15 -0
  69. package/node_modules/graceful-fs/README.md +143 -0
  70. package/node_modules/graceful-fs/clone.js +23 -0
  71. package/node_modules/graceful-fs/graceful-fs.js +448 -0
  72. package/node_modules/graceful-fs/legacy-streams.js +118 -0
  73. package/node_modules/graceful-fs/package.json +50 -0
  74. package/node_modules/graceful-fs/polyfills.js +355 -0
  75. package/node_modules/jsonfile/CHANGELOG.md +171 -0
  76. package/node_modules/jsonfile/LICENSE +15 -0
  77. package/node_modules/jsonfile/README.md +230 -0
  78. package/node_modules/jsonfile/index.js +88 -0
  79. package/node_modules/jsonfile/package.json +40 -0
  80. package/node_modules/jsonfile/utils.js +14 -0
  81. package/node_modules/universalify/LICENSE +20 -0
  82. package/node_modules/universalify/README.md +76 -0
  83. package/node_modules/universalify/index.js +24 -0
  84. package/node_modules/universalify/package.json +34 -0
  85. package/package.json +17 -9
@@ -0,0 +1,262 @@
1
+ Node.js: fs-extra
2
+ =================
3
+
4
+ `fs-extra` adds file system methods that aren't included in the native `fs` module and adds promise support to the `fs` methods. It also uses [`graceful-fs`](https://github.com/isaacs/node-graceful-fs) to prevent `EMFILE` errors. It should be a drop in replacement for `fs`.
5
+
6
+ [![npm Package](https://img.shields.io/npm/v/fs-extra.svg)](https://www.npmjs.org/package/fs-extra)
7
+ [![License](https://img.shields.io/npm/l/fs-extra.svg)](https://github.com/jprichardson/node-fs-extra/blob/master/LICENSE)
8
+ [![build status](https://img.shields.io/github/workflow/status/jprichardson/node-fs-extra/Node.js%20CI/master)](https://github.com/jprichardson/node-fs-extra/actions/workflows/ci.yml?query=branch%3Amaster)
9
+ [![downloads per month](http://img.shields.io/npm/dm/fs-extra.svg)](https://www.npmjs.org/package/fs-extra)
10
+ [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
11
+
12
+ Why?
13
+ ----
14
+
15
+ I got tired of including `mkdirp`, `rimraf`, and `ncp` in most of my projects.
16
+
17
+
18
+
19
+
20
+ Installation
21
+ ------------
22
+
23
+ npm install fs-extra
24
+
25
+
26
+
27
+ Usage
28
+ -----
29
+
30
+ `fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are attached to `fs-extra`. All `fs` methods return promises if the callback isn't passed.
31
+
32
+ You don't ever need to include the original `fs` module again:
33
+
34
+ ```js
35
+ const fs = require('fs') // this is no longer necessary
36
+ ```
37
+
38
+ you can now do this:
39
+
40
+ ```js
41
+ const fs = require('fs-extra')
42
+ ```
43
+
44
+ or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want
45
+ to name your `fs` variable `fse` like so:
46
+
47
+ ```js
48
+ const fse = require('fs-extra')
49
+ ```
50
+
51
+ you can also keep both, but it's redundant:
52
+
53
+ ```js
54
+ const fs = require('fs')
55
+ const fse = require('fs-extra')
56
+ ```
57
+
58
+ Sync vs Async vs Async/Await
59
+ -------------
60
+ Most methods are async by default. All async methods will return a promise if the callback isn't passed.
61
+
62
+ Sync methods on the other hand will throw if an error occurs.
63
+
64
+ Also Async/Await will throw an error if one occurs.
65
+
66
+ Example:
67
+
68
+ ```js
69
+ const fs = require('fs-extra')
70
+
71
+ // Async with promises:
72
+ fs.copy('/tmp/myfile', '/tmp/mynewfile')
73
+ .then(() => console.log('success!'))
74
+ .catch(err => console.error(err))
75
+
76
+ // Async with callbacks:
77
+ fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
78
+ if (err) return console.error(err)
79
+ console.log('success!')
80
+ })
81
+
82
+ // Sync:
83
+ try {
84
+ fs.copySync('/tmp/myfile', '/tmp/mynewfile')
85
+ console.log('success!')
86
+ } catch (err) {
87
+ console.error(err)
88
+ }
89
+
90
+ // Async/Await:
91
+ async function copyFiles () {
92
+ try {
93
+ await fs.copy('/tmp/myfile', '/tmp/mynewfile')
94
+ console.log('success!')
95
+ } catch (err) {
96
+ console.error(err)
97
+ }
98
+ }
99
+
100
+ copyFiles()
101
+ ```
102
+
103
+
104
+ Methods
105
+ -------
106
+
107
+ ### Async
108
+
109
+ - [copy](docs/copy.md)
110
+ - [emptyDir](docs/emptyDir.md)
111
+ - [ensureFile](docs/ensureFile.md)
112
+ - [ensureDir](docs/ensureDir.md)
113
+ - [ensureLink](docs/ensureLink.md)
114
+ - [ensureSymlink](docs/ensureSymlink.md)
115
+ - [mkdirp](docs/ensureDir.md)
116
+ - [mkdirs](docs/ensureDir.md)
117
+ - [move](docs/move.md)
118
+ - [outputFile](docs/outputFile.md)
119
+ - [outputJson](docs/outputJson.md)
120
+ - [pathExists](docs/pathExists.md)
121
+ - [readJson](docs/readJson.md)
122
+ - [remove](docs/remove.md)
123
+ - [writeJson](docs/writeJson.md)
124
+
125
+ ### Sync
126
+
127
+ - [copySync](docs/copy-sync.md)
128
+ - [emptyDirSync](docs/emptyDir-sync.md)
129
+ - [ensureFileSync](docs/ensureFile-sync.md)
130
+ - [ensureDirSync](docs/ensureDir-sync.md)
131
+ - [ensureLinkSync](docs/ensureLink-sync.md)
132
+ - [ensureSymlinkSync](docs/ensureSymlink-sync.md)
133
+ - [mkdirpSync](docs/ensureDir-sync.md)
134
+ - [mkdirsSync](docs/ensureDir-sync.md)
135
+ - [moveSync](docs/move-sync.md)
136
+ - [outputFileSync](docs/outputFile-sync.md)
137
+ - [outputJsonSync](docs/outputJson-sync.md)
138
+ - [pathExistsSync](docs/pathExists-sync.md)
139
+ - [readJsonSync](docs/readJson-sync.md)
140
+ - [removeSync](docs/remove-sync.md)
141
+ - [writeJsonSync](docs/writeJson-sync.md)
142
+
143
+
144
+ **NOTE:** You can still use the native Node.js methods. They are promisified and copied over to `fs-extra`. See [notes on `fs.read()`, `fs.write()`, & `fs.writev()`](docs/fs-read-write-writev.md)
145
+
146
+ ### What happened to `walk()` and `walkSync()`?
147
+
148
+ They were removed from `fs-extra` in v2.0.0. If you need the functionality, `walk` and `walkSync` are available as separate packages, [`klaw`](https://github.com/jprichardson/node-klaw) and [`klaw-sync`](https://github.com/manidlou/node-klaw-sync).
149
+
150
+
151
+ Third Party
152
+ -----------
153
+
154
+ ### CLI
155
+
156
+ [fse-cli](https://www.npmjs.com/package/@atao60/fse-cli) allows you to run `fs-extra` from a console or from [npm](https://www.npmjs.com) scripts.
157
+
158
+ ### TypeScript
159
+
160
+ If you like TypeScript, you can use `fs-extra` with it: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/fs-extra
161
+
162
+
163
+ ### File / Directory Watching
164
+
165
+ If you want to watch for changes to files or directories, then you should use [chokidar](https://github.com/paulmillr/chokidar).
166
+
167
+ ### Obtain Filesystem (Devices, Partitions) Information
168
+
169
+ [fs-filesystem](https://github.com/arthurintelligence/node-fs-filesystem) allows you to read the state of the filesystem of the host on which it is run. It returns information about both the devices and the partitions (volumes) of the system.
170
+
171
+ ### Misc.
172
+
173
+ - [fs-extra-debug](https://github.com/jdxcode/fs-extra-debug) - Send your fs-extra calls to [debug](https://npmjs.org/package/debug).
174
+ - [mfs](https://github.com/cadorn/mfs) - Monitor your fs-extra calls.
175
+
176
+
177
+
178
+ Hacking on fs-extra
179
+ -------------------
180
+
181
+ Wanna hack on `fs-extra`? Great! Your help is needed! [fs-extra is one of the most depended upon Node.js packages](http://nodei.co/npm/fs-extra.png?downloads=true&downloadRank=true&stars=true). This project
182
+ uses [JavaScript Standard Style](https://github.com/feross/standard) - if the name or style choices bother you,
183
+ you're gonna have to get over it :) If `standard` is good enough for `npm`, it's good enough for `fs-extra`.
184
+
185
+ [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
186
+
187
+ What's needed?
188
+ - First, take a look at existing issues. Those are probably going to be where the priority lies.
189
+ - More tests for edge cases. Specifically on different platforms. There can never be enough tests.
190
+ - Improve test coverage.
191
+
192
+ Note: If you make any big changes, **you should definitely file an issue for discussion first.**
193
+
194
+ ### Running the Test Suite
195
+
196
+ fs-extra contains hundreds of tests.
197
+
198
+ - `npm run lint`: runs the linter ([standard](http://standardjs.com/))
199
+ - `npm run unit`: runs the unit tests
200
+ - `npm test`: runs both the linter and the tests
201
+
202
+
203
+ ### Windows
204
+
205
+ If you run the tests on the Windows and receive a lot of symbolic link `EPERM` permission errors, it's
206
+ because on Windows you need elevated privilege to create symbolic links. You can add this to your Windows's
207
+ account by following the instructions here: http://superuser.com/questions/104845/permission-to-make-symbolic-links-in-windows-7
208
+ However, I didn't have much luck doing this.
209
+
210
+ Since I develop on Mac OS X, I use VMWare Fusion for Windows testing. I create a shared folder that I map to a drive on Windows.
211
+ I open the `Node.js command prompt` and run as `Administrator`. I then map the network drive running the following command:
212
+
213
+ net use z: "\\vmware-host\Shared Folders"
214
+
215
+ I can then navigate to my `fs-extra` directory and run the tests.
216
+
217
+
218
+ Naming
219
+ ------
220
+
221
+ I put a lot of thought into the naming of these functions. Inspired by @coolaj86's request. So he deserves much of the credit for raising the issue. See discussion(s) here:
222
+
223
+ * https://github.com/jprichardson/node-fs-extra/issues/2
224
+ * https://github.com/flatiron/utile/issues/11
225
+ * https://github.com/ryanmcgrath/wrench-js/issues/29
226
+ * https://github.com/substack/node-mkdirp/issues/17
227
+
228
+ First, I believe that in as many cases as possible, the [Node.js naming schemes](http://nodejs.org/api/fs.html) should be chosen. However, there are problems with the Node.js own naming schemes.
229
+
230
+ For example, `fs.readFile()` and `fs.readdir()`: the **F** is capitalized in *File* and the **d** is not capitalized in *dir*. Perhaps a bit pedantic, but they should still be consistent. Also, Node.js has chosen a lot of POSIX naming schemes, which I believe is great. See: `fs.mkdir()`, `fs.rmdir()`, `fs.chown()`, etc.
231
+
232
+ We have a dilemma though. How do you consistently name methods that perform the following POSIX commands: `cp`, `cp -r`, `mkdir -p`, and `rm -rf`?
233
+
234
+ My perspective: when in doubt, err on the side of simplicity. A directory is just a hierarchical grouping of directories and files. Consider that for a moment. So when you want to copy it or remove it, in most cases you'll want to copy or remove all of its contents. When you want to create a directory, if the directory that it's suppose to be contained in does not exist, then in most cases you'll want to create that too.
235
+
236
+ So, if you want to remove a file or a directory regardless of whether it has contents, just call `fs.remove(path)`. If you want to copy a file or a directory whether it has contents, just call `fs.copy(source, destination)`. If you want to create a directory regardless of whether its parent directories exist, just call `fs.mkdirs(path)` or `fs.mkdirp(path)`.
237
+
238
+
239
+ Credit
240
+ ------
241
+
242
+ `fs-extra` wouldn't be possible without using the modules from the following authors:
243
+
244
+ - [Isaac Shlueter](https://github.com/isaacs)
245
+ - [Charlie McConnel](https://github.com/avianflu)
246
+ - [James Halliday](https://github.com/substack)
247
+ - [Andrew Kelley](https://github.com/andrewrk)
248
+
249
+
250
+
251
+
252
+ License
253
+ -------
254
+
255
+ Licensed under MIT
256
+
257
+ Copyright (c) 2011-2017 [JP Richardson](https://github.com/jprichardson)
258
+
259
+ [1]: http://nodejs.org/docs/latest/api/fs.html
260
+
261
+
262
+ [jsonfile]: https://github.com/jprichardson/node-jsonfile
@@ -0,0 +1,169 @@
1
+ 'use strict'
2
+
3
+ const fs = require('graceful-fs')
4
+ const path = require('path')
5
+ const mkdirsSync = require('../mkdirs').mkdirsSync
6
+ const utimesMillisSync = require('../util/utimes').utimesMillisSync
7
+ const stat = require('../util/stat')
8
+
9
+ function copySync (src, dest, opts) {
10
+ if (typeof opts === 'function') {
11
+ opts = { filter: opts }
12
+ }
13
+
14
+ opts = opts || {}
15
+ opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
16
+ opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
17
+
18
+ // Warn about using preserveTimestamps on 32-bit node
19
+ if (opts.preserveTimestamps && process.arch === 'ia32') {
20
+ process.emitWarning(
21
+ 'Using the preserveTimestamps option in 32-bit node is not recommended;\n\n' +
22
+ '\tsee https://github.com/jprichardson/node-fs-extra/issues/269',
23
+ 'Warning', 'fs-extra-WARN0002'
24
+ )
25
+ }
26
+
27
+ const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
28
+ stat.checkParentPathsSync(src, srcStat, dest, 'copy')
29
+ return handleFilterAndCopy(destStat, src, dest, opts)
30
+ }
31
+
32
+ function handleFilterAndCopy (destStat, src, dest, opts) {
33
+ if (opts.filter && !opts.filter(src, dest)) return
34
+ const destParent = path.dirname(dest)
35
+ if (!fs.existsSync(destParent)) mkdirsSync(destParent)
36
+ return getStats(destStat, src, dest, opts)
37
+ }
38
+
39
+ function startCopy (destStat, src, dest, opts) {
40
+ if (opts.filter && !opts.filter(src, dest)) return
41
+ return getStats(destStat, src, dest, opts)
42
+ }
43
+
44
+ function getStats (destStat, src, dest, opts) {
45
+ const statSync = opts.dereference ? fs.statSync : fs.lstatSync
46
+ const srcStat = statSync(src)
47
+
48
+ if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
49
+ else if (srcStat.isFile() ||
50
+ srcStat.isCharacterDevice() ||
51
+ srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
52
+ else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
53
+ else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
54
+ else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
55
+ throw new Error(`Unknown file: ${src}`)
56
+ }
57
+
58
+ function onFile (srcStat, destStat, src, dest, opts) {
59
+ if (!destStat) return copyFile(srcStat, src, dest, opts)
60
+ return mayCopyFile(srcStat, src, dest, opts)
61
+ }
62
+
63
+ function mayCopyFile (srcStat, src, dest, opts) {
64
+ if (opts.overwrite) {
65
+ fs.unlinkSync(dest)
66
+ return copyFile(srcStat, src, dest, opts)
67
+ } else if (opts.errorOnExist) {
68
+ throw new Error(`'${dest}' already exists`)
69
+ }
70
+ }
71
+
72
+ function copyFile (srcStat, src, dest, opts) {
73
+ fs.copyFileSync(src, dest)
74
+ if (opts.preserveTimestamps) handleTimestamps(srcStat.mode, src, dest)
75
+ return setDestMode(dest, srcStat.mode)
76
+ }
77
+
78
+ function handleTimestamps (srcMode, src, dest) {
79
+ // Make sure the file is writable before setting the timestamp
80
+ // otherwise open fails with EPERM when invoked with 'r+'
81
+ // (through utimes call)
82
+ if (fileIsNotWritable(srcMode)) makeFileWritable(dest, srcMode)
83
+ return setDestTimestamps(src, dest)
84
+ }
85
+
86
+ function fileIsNotWritable (srcMode) {
87
+ return (srcMode & 0o200) === 0
88
+ }
89
+
90
+ function makeFileWritable (dest, srcMode) {
91
+ return setDestMode(dest, srcMode | 0o200)
92
+ }
93
+
94
+ function setDestMode (dest, srcMode) {
95
+ return fs.chmodSync(dest, srcMode)
96
+ }
97
+
98
+ function setDestTimestamps (src, dest) {
99
+ // The initial srcStat.atime cannot be trusted
100
+ // because it is modified by the read(2) system call
101
+ // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
102
+ const updatedSrcStat = fs.statSync(src)
103
+ return utimesMillisSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
104
+ }
105
+
106
+ function onDir (srcStat, destStat, src, dest, opts) {
107
+ if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
108
+ return copyDir(src, dest, opts)
109
+ }
110
+
111
+ function mkDirAndCopy (srcMode, src, dest, opts) {
112
+ fs.mkdirSync(dest)
113
+ copyDir(src, dest, opts)
114
+ return setDestMode(dest, srcMode)
115
+ }
116
+
117
+ function copyDir (src, dest, opts) {
118
+ fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
119
+ }
120
+
121
+ function copyDirItem (item, src, dest, opts) {
122
+ const srcItem = path.join(src, item)
123
+ const destItem = path.join(dest, item)
124
+ const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
125
+ return startCopy(destStat, srcItem, destItem, opts)
126
+ }
127
+
128
+ function onLink (destStat, src, dest, opts) {
129
+ let resolvedSrc = fs.readlinkSync(src)
130
+ if (opts.dereference) {
131
+ resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
132
+ }
133
+
134
+ if (!destStat) {
135
+ return fs.symlinkSync(resolvedSrc, dest)
136
+ } else {
137
+ let resolvedDest
138
+ try {
139
+ resolvedDest = fs.readlinkSync(dest)
140
+ } catch (err) {
141
+ // dest exists and is a regular file or directory,
142
+ // Windows may throw UNKNOWN error. If dest already exists,
143
+ // fs throws error anyway, so no need to guard against it here.
144
+ if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
145
+ throw err
146
+ }
147
+ if (opts.dereference) {
148
+ resolvedDest = path.resolve(process.cwd(), resolvedDest)
149
+ }
150
+ if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
151
+ throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
152
+ }
153
+
154
+ // prevent copy if src is a subdir of dest since unlinking
155
+ // dest in this case would result in removing src contents
156
+ // and therefore a broken symlink would be created.
157
+ if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
158
+ throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
159
+ }
160
+ return copyLink(resolvedSrc, dest)
161
+ }
162
+ }
163
+
164
+ function copyLink (resolvedSrc, dest) {
165
+ fs.unlinkSync(dest)
166
+ return fs.symlinkSync(resolvedSrc, dest)
167
+ }
168
+
169
+ module.exports = copySync
@@ -0,0 +1,235 @@
1
+ 'use strict'
2
+
3
+ const fs = require('graceful-fs')
4
+ const path = require('path')
5
+ const mkdirs = require('../mkdirs').mkdirs
6
+ const pathExists = require('../path-exists').pathExists
7
+ const utimesMillis = require('../util/utimes').utimesMillis
8
+ const stat = require('../util/stat')
9
+
10
+ function copy (src, dest, opts, cb) {
11
+ if (typeof opts === 'function' && !cb) {
12
+ cb = opts
13
+ opts = {}
14
+ } else if (typeof opts === 'function') {
15
+ opts = { filter: opts }
16
+ }
17
+
18
+ cb = cb || function () {}
19
+ opts = opts || {}
20
+
21
+ opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
22
+ opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
23
+
24
+ // Warn about using preserveTimestamps on 32-bit node
25
+ if (opts.preserveTimestamps && process.arch === 'ia32') {
26
+ process.emitWarning(
27
+ 'Using the preserveTimestamps option in 32-bit node is not recommended;\n\n' +
28
+ '\tsee https://github.com/jprichardson/node-fs-extra/issues/269',
29
+ 'Warning', 'fs-extra-WARN0001'
30
+ )
31
+ }
32
+
33
+ stat.checkPaths(src, dest, 'copy', opts, (err, stats) => {
34
+ if (err) return cb(err)
35
+ const { srcStat, destStat } = stats
36
+ stat.checkParentPaths(src, srcStat, dest, 'copy', err => {
37
+ if (err) return cb(err)
38
+ if (opts.filter) return handleFilter(checkParentDir, destStat, src, dest, opts, cb)
39
+ return checkParentDir(destStat, src, dest, opts, cb)
40
+ })
41
+ })
42
+ }
43
+
44
+ function checkParentDir (destStat, src, dest, opts, cb) {
45
+ const destParent = path.dirname(dest)
46
+ pathExists(destParent, (err, dirExists) => {
47
+ if (err) return cb(err)
48
+ if (dirExists) return getStats(destStat, src, dest, opts, cb)
49
+ mkdirs(destParent, err => {
50
+ if (err) return cb(err)
51
+ return getStats(destStat, src, dest, opts, cb)
52
+ })
53
+ })
54
+ }
55
+
56
+ function handleFilter (onInclude, destStat, src, dest, opts, cb) {
57
+ Promise.resolve(opts.filter(src, dest)).then(include => {
58
+ if (include) return onInclude(destStat, src, dest, opts, cb)
59
+ return cb()
60
+ }, error => cb(error))
61
+ }
62
+
63
+ function startCopy (destStat, src, dest, opts, cb) {
64
+ if (opts.filter) return handleFilter(getStats, destStat, src, dest, opts, cb)
65
+ return getStats(destStat, src, dest, opts, cb)
66
+ }
67
+
68
+ function getStats (destStat, src, dest, opts, cb) {
69
+ const stat = opts.dereference ? fs.stat : fs.lstat
70
+ stat(src, (err, srcStat) => {
71
+ if (err) return cb(err)
72
+
73
+ if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts, cb)
74
+ else if (srcStat.isFile() ||
75
+ srcStat.isCharacterDevice() ||
76
+ srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts, cb)
77
+ else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts, cb)
78
+ else if (srcStat.isSocket()) return cb(new Error(`Cannot copy a socket file: ${src}`))
79
+ else if (srcStat.isFIFO()) return cb(new Error(`Cannot copy a FIFO pipe: ${src}`))
80
+ return cb(new Error(`Unknown file: ${src}`))
81
+ })
82
+ }
83
+
84
+ function onFile (srcStat, destStat, src, dest, opts, cb) {
85
+ if (!destStat) return copyFile(srcStat, src, dest, opts, cb)
86
+ return mayCopyFile(srcStat, src, dest, opts, cb)
87
+ }
88
+
89
+ function mayCopyFile (srcStat, src, dest, opts, cb) {
90
+ if (opts.overwrite) {
91
+ fs.unlink(dest, err => {
92
+ if (err) return cb(err)
93
+ return copyFile(srcStat, src, dest, opts, cb)
94
+ })
95
+ } else if (opts.errorOnExist) {
96
+ return cb(new Error(`'${dest}' already exists`))
97
+ } else return cb()
98
+ }
99
+
100
+ function copyFile (srcStat, src, dest, opts, cb) {
101
+ fs.copyFile(src, dest, err => {
102
+ if (err) return cb(err)
103
+ if (opts.preserveTimestamps) return handleTimestampsAndMode(srcStat.mode, src, dest, cb)
104
+ return setDestMode(dest, srcStat.mode, cb)
105
+ })
106
+ }
107
+
108
+ function handleTimestampsAndMode (srcMode, src, dest, cb) {
109
+ // Make sure the file is writable before setting the timestamp
110
+ // otherwise open fails with EPERM when invoked with 'r+'
111
+ // (through utimes call)
112
+ if (fileIsNotWritable(srcMode)) {
113
+ return makeFileWritable(dest, srcMode, err => {
114
+ if (err) return cb(err)
115
+ return setDestTimestampsAndMode(srcMode, src, dest, cb)
116
+ })
117
+ }
118
+ return setDestTimestampsAndMode(srcMode, src, dest, cb)
119
+ }
120
+
121
+ function fileIsNotWritable (srcMode) {
122
+ return (srcMode & 0o200) === 0
123
+ }
124
+
125
+ function makeFileWritable (dest, srcMode, cb) {
126
+ return setDestMode(dest, srcMode | 0o200, cb)
127
+ }
128
+
129
+ function setDestTimestampsAndMode (srcMode, src, dest, cb) {
130
+ setDestTimestamps(src, dest, err => {
131
+ if (err) return cb(err)
132
+ return setDestMode(dest, srcMode, cb)
133
+ })
134
+ }
135
+
136
+ function setDestMode (dest, srcMode, cb) {
137
+ return fs.chmod(dest, srcMode, cb)
138
+ }
139
+
140
+ function setDestTimestamps (src, dest, cb) {
141
+ // The initial srcStat.atime cannot be trusted
142
+ // because it is modified by the read(2) system call
143
+ // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
144
+ fs.stat(src, (err, updatedSrcStat) => {
145
+ if (err) return cb(err)
146
+ return utimesMillis(dest, updatedSrcStat.atime, updatedSrcStat.mtime, cb)
147
+ })
148
+ }
149
+
150
+ function onDir (srcStat, destStat, src, dest, opts, cb) {
151
+ if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, cb)
152
+ return copyDir(src, dest, opts, cb)
153
+ }
154
+
155
+ function mkDirAndCopy (srcMode, src, dest, opts, cb) {
156
+ fs.mkdir(dest, err => {
157
+ if (err) return cb(err)
158
+ copyDir(src, dest, opts, err => {
159
+ if (err) return cb(err)
160
+ return setDestMode(dest, srcMode, cb)
161
+ })
162
+ })
163
+ }
164
+
165
+ function copyDir (src, dest, opts, cb) {
166
+ fs.readdir(src, (err, items) => {
167
+ if (err) return cb(err)
168
+ return copyDirItems(items, src, dest, opts, cb)
169
+ })
170
+ }
171
+
172
+ function copyDirItems (items, src, dest, opts, cb) {
173
+ const item = items.pop()
174
+ if (!item) return cb()
175
+ return copyDirItem(items, item, src, dest, opts, cb)
176
+ }
177
+
178
+ function copyDirItem (items, item, src, dest, opts, cb) {
179
+ const srcItem = path.join(src, item)
180
+ const destItem = path.join(dest, item)
181
+ stat.checkPaths(srcItem, destItem, 'copy', opts, (err, stats) => {
182
+ if (err) return cb(err)
183
+ const { destStat } = stats
184
+ startCopy(destStat, srcItem, destItem, opts, err => {
185
+ if (err) return cb(err)
186
+ return copyDirItems(items, src, dest, opts, cb)
187
+ })
188
+ })
189
+ }
190
+
191
+ function onLink (destStat, src, dest, opts, cb) {
192
+ fs.readlink(src, (err, resolvedSrc) => {
193
+ if (err) return cb(err)
194
+ if (opts.dereference) {
195
+ resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
196
+ }
197
+
198
+ if (!destStat) {
199
+ return fs.symlink(resolvedSrc, dest, cb)
200
+ } else {
201
+ fs.readlink(dest, (err, resolvedDest) => {
202
+ if (err) {
203
+ // dest exists and is a regular file or directory,
204
+ // Windows may throw UNKNOWN error. If dest already exists,
205
+ // fs throws error anyway, so no need to guard against it here.
206
+ if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlink(resolvedSrc, dest, cb)
207
+ return cb(err)
208
+ }
209
+ if (opts.dereference) {
210
+ resolvedDest = path.resolve(process.cwd(), resolvedDest)
211
+ }
212
+ if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
213
+ return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`))
214
+ }
215
+
216
+ // do not copy if src is a subdir of dest since unlinking
217
+ // dest in this case would result in removing src contents
218
+ // and therefore a broken symlink would be created.
219
+ if (destStat.isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
220
+ return cb(new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`))
221
+ }
222
+ return copyLink(resolvedSrc, dest, cb)
223
+ })
224
+ }
225
+ })
226
+ }
227
+
228
+ function copyLink (resolvedSrc, dest, cb) {
229
+ fs.unlink(dest, err => {
230
+ if (err) return cb(err)
231
+ return fs.symlink(resolvedSrc, dest, cb)
232
+ })
233
+ }
234
+
235
+ module.exports = copy
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const u = require('universalify').fromCallback
4
+ module.exports = {
5
+ copy: u(require('./copy')),
6
+ copySync: require('./copy-sync')
7
+ }