|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// node-enumerable (https://github.com/mkloubert/node-enumerable) |
| 4 | +// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net> |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to |
| 8 | +// deal in the Software without restriction, including without limitation the |
| 9 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +// DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | + |
| 25 | +// this is a script target for |
| 26 | +// vs-deploy VSCode extension |
| 27 | +// |
| 28 | +// s. https://github.com/mkloubert/vs-deploy |
| 29 | +// for more information |
| 30 | + |
| 31 | +const FS = require('fs'); |
| 32 | +const Path = require('path'); |
| 33 | +const vscode = require('vscode'); |
| 34 | + |
| 35 | +exports.deployWorkspace = function(args) { |
| 36 | + const FSExtra = args.require('fs-extra'); |
| 37 | + |
| 38 | + // workspace & 'releases' folders |
| 39 | + const WORKSPACE = Path.resolve( |
| 40 | + Path.join(__dirname, '../') |
| 41 | + ); |
| 42 | + const RELEASES = Path.resolve( |
| 43 | + Path.join(__dirname, './releases') |
| 44 | + ); |
| 45 | + |
| 46 | + // package.json |
| 47 | + const PACKAGE_JSON_FILE = Path.resolve( |
| 48 | + Path.join(WORKSPACE, 'package.json') |
| 49 | + ); |
| 50 | + const PACKAGE_JSON = JSON.parse( |
| 51 | + FS.readFileSync(PACKAGE_JSON_FILE) |
| 52 | + .toString('utf8') |
| 53 | + ); |
| 54 | + |
| 55 | + // module version |
| 56 | + const VERSION = ('' + PACKAGE_JSON.version).trim(); |
| 57 | + |
| 58 | + // output directory |
| 59 | + const OUT_DIR = Path.resolve( |
| 60 | + Path.join(RELEASES, VERSION) |
| 61 | + ); |
| 62 | + |
| 63 | + // keep sure to have a clean |
| 64 | + // and empty 'releases' folder |
| 65 | + if (FSExtra.existsSync(RELEASES)) { |
| 66 | + FSExtra.removeSync( RELEASES ); |
| 67 | + } |
| 68 | + FSExtra.mkdirsSync( OUT_DIR ); |
| 69 | + |
| 70 | + // ${workspace}/js/enumerable.js |
| 71 | + const ENUMERABLE_JS = Path.resolve( |
| 72 | + Path.join(WORKSPACE, 'js/enumerable.js') |
| 73 | + ); |
| 74 | + |
| 75 | + for (let i = 0; i < args.files.length; i++) { |
| 76 | + try { |
| 77 | + args.onBeforeDeployFile(i); |
| 78 | + |
| 79 | + const FILE = args.files[i]; |
| 80 | + |
| 81 | + const SOURCE_FILE = Path.resolve(FILE); |
| 82 | + const REL_PATH = Path.relative(WORKSPACE, SOURCE_FILE); |
| 83 | + |
| 84 | + let targetFile; |
| 85 | + if (SOURCE_FILE === ENUMERABLE_JS) { |
| 86 | + // map 'js/enumerable.js' |
| 87 | + // to '/index.js' instead |
| 88 | + targetFile = Path.join(OUT_DIR, 'index.js'); |
| 89 | + } |
| 90 | + else { |
| 91 | + targetFile = Path.join(OUT_DIR, REL_PATH); |
| 92 | + } |
| 93 | + targetFile = Path.resolve(targetFile); |
| 94 | + |
| 95 | + // target directory for current file |
| 96 | + const TARGET_DIR = Path.resolve( |
| 97 | + Path.join(Path.dirname(targetFile)) |
| 98 | + ); |
| 99 | + if (!FSExtra.existsSync(TARGET_DIR)) { |
| 100 | + FSExtra.mkdirsSync( TARGET_DIR ); |
| 101 | + } |
| 102 | + |
| 103 | + // copy current file to target |
| 104 | + FSExtra.copySync(SOURCE_FILE, targetFile, { |
| 105 | + overwrite: false, |
| 106 | + errorOnExist: true, |
| 107 | + dereference: true |
| 108 | + }); |
| 109 | + |
| 110 | + args.onFileCompleted(i); |
| 111 | + } |
| 112 | + catch (e) { |
| 113 | + args.onFileCompleted(i, e); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments