io.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const childProcess = require("child_process");
  13. const path = require("path");
  14. const util_1 = require("util");
  15. const ioUtil = require("./io-util");
  16. const exec = util_1.promisify(childProcess.exec);
  17. /**
  18. * Copies a file or folder.
  19. * Based off of shelljs - https://github.com/shelljs/shelljs/blob/9237f66c52e5daa40458f94f9565e18e8132f5a6/src/cp.js
  20. *
  21. * @param source source path
  22. * @param dest destination path
  23. * @param options optional. See CopyOptions.
  24. */
  25. function cp(source, dest, options = {}) {
  26. return __awaiter(this, void 0, void 0, function* () {
  27. const { force, recursive } = readCopyOptions(options);
  28. const destStat = (yield ioUtil.exists(dest)) ? yield ioUtil.stat(dest) : null;
  29. // Dest is an existing file, but not forcing
  30. if (destStat && destStat.isFile() && !force) {
  31. return;
  32. }
  33. // If dest is an existing directory, should copy inside.
  34. const newDest = destStat && destStat.isDirectory()
  35. ? path.join(dest, path.basename(source))
  36. : dest;
  37. if (!(yield ioUtil.exists(source))) {
  38. throw new Error(`no such file or directory: ${source}`);
  39. }
  40. const sourceStat = yield ioUtil.stat(source);
  41. if (sourceStat.isDirectory()) {
  42. if (!recursive) {
  43. throw new Error(`Failed to copy. ${source} is a directory, but tried to copy without recursive flag.`);
  44. }
  45. else {
  46. yield cpDirRecursive(source, newDest, 0, force);
  47. }
  48. }
  49. else {
  50. if (path.relative(source, newDest) === '') {
  51. // a file cannot be copied to itself
  52. throw new Error(`'${newDest}' and '${source}' are the same file`);
  53. }
  54. yield copyFile(source, newDest, force);
  55. }
  56. });
  57. }
  58. exports.cp = cp;
  59. /**
  60. * Moves a path.
  61. *
  62. * @param source source path
  63. * @param dest destination path
  64. * @param options optional. See MoveOptions.
  65. */
  66. function mv(source, dest, options = {}) {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. if (yield ioUtil.exists(dest)) {
  69. let destExists = true;
  70. if (yield ioUtil.isDirectory(dest)) {
  71. // If dest is directory copy src into dest
  72. dest = path.join(dest, path.basename(source));
  73. destExists = yield ioUtil.exists(dest);
  74. }
  75. if (destExists) {
  76. if (options.force == null || options.force) {
  77. yield rmRF(dest);
  78. }
  79. else {
  80. throw new Error('Destination already exists');
  81. }
  82. }
  83. }
  84. yield mkdirP(path.dirname(dest));
  85. yield ioUtil.rename(source, dest);
  86. });
  87. }
  88. exports.mv = mv;
  89. /**
  90. * Remove a path recursively with force
  91. *
  92. * @param inputPath path to remove
  93. */
  94. function rmRF(inputPath) {
  95. return __awaiter(this, void 0, void 0, function* () {
  96. if (ioUtil.IS_WINDOWS) {
  97. // Node doesn't provide a delete operation, only an unlink function. This means that if the file is being used by another
  98. // program (e.g. antivirus), it won't be deleted. To address this, we shell out the work to rd/del.
  99. try {
  100. if (yield ioUtil.isDirectory(inputPath, true)) {
  101. yield exec(`rd /s /q "${inputPath}"`);
  102. }
  103. else {
  104. yield exec(`del /f /a "${inputPath}"`);
  105. }
  106. }
  107. catch (err) {
  108. // if you try to delete a file that doesn't exist, desired result is achieved
  109. // other errors are valid
  110. if (err.code !== 'ENOENT')
  111. throw err;
  112. }
  113. // Shelling out fails to remove a symlink folder with missing source, this unlink catches that
  114. try {
  115. yield ioUtil.unlink(inputPath);
  116. }
  117. catch (err) {
  118. // if you try to delete a file that doesn't exist, desired result is achieved
  119. // other errors are valid
  120. if (err.code !== 'ENOENT')
  121. throw err;
  122. }
  123. }
  124. else {
  125. let isDir = false;
  126. try {
  127. isDir = yield ioUtil.isDirectory(inputPath);
  128. }
  129. catch (err) {
  130. // if you try to delete a file that doesn't exist, desired result is achieved
  131. // other errors are valid
  132. if (err.code !== 'ENOENT')
  133. throw err;
  134. return;
  135. }
  136. if (isDir) {
  137. yield exec(`rm -rf "${inputPath}"`);
  138. }
  139. else {
  140. yield ioUtil.unlink(inputPath);
  141. }
  142. }
  143. });
  144. }
  145. exports.rmRF = rmRF;
  146. /**
  147. * Make a directory. Creates the full path with folders in between
  148. * Will throw if it fails
  149. *
  150. * @param fsPath path to create
  151. * @returns Promise<void>
  152. */
  153. function mkdirP(fsPath) {
  154. return __awaiter(this, void 0, void 0, function* () {
  155. yield ioUtil.mkdirP(fsPath);
  156. });
  157. }
  158. exports.mkdirP = mkdirP;
  159. /**
  160. * Returns path of a tool had the tool actually been invoked. Resolves via paths.
  161. * If you check and the tool does not exist, it will throw.
  162. *
  163. * @param tool name of the tool
  164. * @param check whether to check if tool exists
  165. * @returns Promise<string> path to tool
  166. */
  167. function which(tool, check) {
  168. return __awaiter(this, void 0, void 0, function* () {
  169. if (!tool) {
  170. throw new Error("parameter 'tool' is required");
  171. }
  172. // recursive when check=true
  173. if (check) {
  174. const result = yield which(tool, false);
  175. if (!result) {
  176. if (ioUtil.IS_WINDOWS) {
  177. throw new Error(`Unable to locate executable file: ${tool}. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.`);
  178. }
  179. else {
  180. throw new Error(`Unable to locate executable file: ${tool}. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.`);
  181. }
  182. }
  183. }
  184. try {
  185. // build the list of extensions to try
  186. const extensions = [];
  187. if (ioUtil.IS_WINDOWS && process.env.PATHEXT) {
  188. for (const extension of process.env.PATHEXT.split(path.delimiter)) {
  189. if (extension) {
  190. extensions.push(extension);
  191. }
  192. }
  193. }
  194. // if it's rooted, return it if exists. otherwise return empty.
  195. if (ioUtil.isRooted(tool)) {
  196. const filePath = yield ioUtil.tryGetExecutablePath(tool, extensions);
  197. if (filePath) {
  198. return filePath;
  199. }
  200. return '';
  201. }
  202. // if any path separators, return empty
  203. if (tool.includes('/') || (ioUtil.IS_WINDOWS && tool.includes('\\'))) {
  204. return '';
  205. }
  206. // build the list of directories
  207. //
  208. // Note, technically "where" checks the current directory on Windows. From a toolkit perspective,
  209. // it feels like we should not do this. Checking the current directory seems like more of a use
  210. // case of a shell, and the which() function exposed by the toolkit should strive for consistency
  211. // across platforms.
  212. const directories = [];
  213. if (process.env.PATH) {
  214. for (const p of process.env.PATH.split(path.delimiter)) {
  215. if (p) {
  216. directories.push(p);
  217. }
  218. }
  219. }
  220. // return the first match
  221. for (const directory of directories) {
  222. const filePath = yield ioUtil.tryGetExecutablePath(directory + path.sep + tool, extensions);
  223. if (filePath) {
  224. return filePath;
  225. }
  226. }
  227. return '';
  228. }
  229. catch (err) {
  230. throw new Error(`which failed with message ${err.message}`);
  231. }
  232. });
  233. }
  234. exports.which = which;
  235. function readCopyOptions(options) {
  236. const force = options.force == null ? true : options.force;
  237. const recursive = Boolean(options.recursive);
  238. return { force, recursive };
  239. }
  240. function cpDirRecursive(sourceDir, destDir, currentDepth, force) {
  241. return __awaiter(this, void 0, void 0, function* () {
  242. // Ensure there is not a run away recursive copy
  243. if (currentDepth >= 255)
  244. return;
  245. currentDepth++;
  246. yield mkdirP(destDir);
  247. const files = yield ioUtil.readdir(sourceDir);
  248. for (const fileName of files) {
  249. const srcFile = `${sourceDir}/${fileName}`;
  250. const destFile = `${destDir}/${fileName}`;
  251. const srcFileStat = yield ioUtil.lstat(srcFile);
  252. if (srcFileStat.isDirectory()) {
  253. // Recurse
  254. yield cpDirRecursive(srcFile, destFile, currentDepth, force);
  255. }
  256. else {
  257. yield copyFile(srcFile, destFile, force);
  258. }
  259. }
  260. // Change the mode for the newly created directory
  261. yield ioUtil.chmod(destDir, (yield ioUtil.stat(sourceDir)).mode);
  262. });
  263. }
  264. // Buffered file copy
  265. function copyFile(srcFile, destFile, force) {
  266. return __awaiter(this, void 0, void 0, function* () {
  267. if ((yield ioUtil.lstat(srcFile)).isSymbolicLink()) {
  268. // unlink/re-link it
  269. try {
  270. yield ioUtil.lstat(destFile);
  271. yield ioUtil.unlink(destFile);
  272. }
  273. catch (e) {
  274. // Try to override file permission
  275. if (e.code === 'EPERM') {
  276. yield ioUtil.chmod(destFile, '0666');
  277. yield ioUtil.unlink(destFile);
  278. }
  279. // other errors = it doesn't exist, no work to do
  280. }
  281. // Copy over symlink
  282. const symlinkFull = yield ioUtil.readlink(srcFile);
  283. yield ioUtil.symlink(symlinkFull, destFile, ioUtil.IS_WINDOWS ? 'junction' : null);
  284. }
  285. else if (!(yield ioUtil.exists(destFile)) || force) {
  286. yield ioUtil.copyFile(srcFile, destFile);
  287. }
  288. });
  289. }
  290. //# sourceMappingURL=io.js.map