From 9df44200c0e6a430d27e7fd3d39e98cbb7ee7154 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 12 Mar 2018 16:15:10 +0100 Subject: [PATCH] Adopt monaco-plugin-helpers --- package-lock.json | 9 +++ package.json | 5 +- scripts/copy.js | 30 ------- scripts/release.js | 198 +++++++-------------------------------------- scripts/rmdir.js | 28 ------- 5 files changed, 39 insertions(+), 231 deletions(-) delete mode 100644 scripts/copy.js delete mode 100644 scripts/rmdir.js diff --git a/package-lock.json b/package-lock.json index 31f20418..c6b5fe29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5518,6 +5518,15 @@ "integrity": "sha1-A+qcUgMbeYN+ejidT8YhHaP3WP0=", "dev": true }, + "monaco-plugin-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/monaco-plugin-helpers/-/monaco-plugin-helpers-1.0.2.tgz", + "integrity": "sha512-7kUx8dtd5qVNVgUARBRhnM8oftPglYwlINfigC4yGUiuzqtIN22u1tly8umiOCIPR0eFiBLjt6aN23oZh2QJgg==", + "dev": true, + "requires": { + "typescript": "2.7.2" + } + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index c2bab1a8..14c1f076 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "1.3.3", "description": "CSS, LESS and SCSS plugin for the Monaco Editor", "scripts": { - "compile": "node ./scripts/rmdir ./out && tsc -p ./src", + "compile": "mrmdir ./out && tsc -p ./src", "watch": "tsc -p ./src --watch", - "prepublish": "node ./scripts/rmdir ./release && npm run compile && node ./scripts/release.js && webpack --config webpack.dev.config.js && webpack --config webpack.min.config.js && node ./scripts/copy ./src/monaco.d.ts ./release/monaco.d.ts", + "prepublish": "mrmdir ./release && npm run compile && node ./scripts/release.js && webpack --config webpack.dev.config.js && webpack --config webpack.min.config.js && mcopy ./src/monaco.d.ts ./release/monaco.d.ts", "install-service-next": "npm install vscode-css-languageservice@next -f -D && npm install vscode-languageserver-types@next -f -D", "install-service-local": "npm install ../vscode-css-languageservice -f -D && npm install ../vscode-languageserver-node/types -f -D" }, @@ -21,6 +21,7 @@ "devDependencies": { "monaco-editor-core": "0.11.1", "monaco-languages": "0.9.0", + "monaco-plugin-helpers": "^1.0.2", "typescript": "2.7.2", "vscode-css-languageservice": "3.0.8", "vscode-languageserver-types": "3.6.1", diff --git a/scripts/copy.js b/scripts/copy.js deleted file mode 100644 index 154d8562..00000000 --- a/scripts/copy.js +++ /dev/null @@ -1,30 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -const fs = require('fs'); -const path = require('path'); - -const source = path.join(process.cwd(), process.argv[2]); -const destination = path.join(process.cwd(), process.argv[3]); - -// ensure target dir -(function () { - let dirs = []; - let dirname = path.dirname(destination); - while (dirname !== process.cwd()) { - dirs.push(dirname); - dirname = path.dirname(dirname); - } - - dirs.reverse(); - - dirs.forEach((dir) => { - try { fs.mkdirSync(dir); } catch (err) { } - }) -})(); - -fs.writeFileSync(destination, fs.readFileSync(source)); - -console.log(`Copied ${process.argv[2]} to ${process.argv[3]}`); diff --git a/scripts/release.js b/scripts/release.js index e4cc2a0b..cf5d429c 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -1,176 +1,32 @@ -"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -exports.__esModule = true; -var fs = require("fs"); -var ts = require("typescript"); -var path = require("path"); -var REPO_ROOT = path.join(__dirname, '../'); -process({ - repoRoot: REPO_ROOT, - esmSource: 'out', - esmDestination: 'release/esm', - entryPoints: [ - 'monaco.contribution.js', - 'cssMode.js', - 'css.worker.js', - ], - resolveAlias: { - 'vscode-nls': path.join(REPO_ROOT, "out/fillers/vscode-nls.js") - }, - resolveSkip: [ - 'monaco-editor-core' - ], - destinationFolderSimplification: { - 'node_modules': '_deps', - 'vscode-languageserver-types/lib/esm': 'vscode-languageserver-types', - 'vscode-uri/lib/esm': 'vscode-uri', - 'vscode-css-languageservice/lib/esm': 'vscode-css-languageservice' - } + +const path = require('path'); +const helpers = require('monaco-plugin-helpers'); + +const REPO_ROOT = path.join(__dirname, '../'); + +helpers.packageESM({ + repoRoot: REPO_ROOT, + esmSource: 'out', + esmDestination: 'release/esm', + entryPoints: [ + 'monaco.contribution.js', + 'cssMode.js', + 'css.worker.js', + ], + resolveAlias: { + 'vscode-nls': path.join(REPO_ROOT, "out/fillers/vscode-nls.js") + }, + resolveSkip: [ + 'monaco-editor-core' + ], + destinationFolderSimplification: { + 'node_modules': '_deps', + 'vscode-languageserver-types/lib/esm': 'vscode-languageserver-types', + 'vscode-uri/lib/esm': 'vscode-uri', + 'vscode-css-languageservice/lib/esm': 'vscode-css-languageservice' + } }); -function process(options) { - options.repoRoot = path.normalize(options.repoRoot).replace(/(\/|\\)$/, ''); - var ESM_SRC = path.join(options.repoRoot, options.esmSource); - var ESM_DEST = path.join(options.repoRoot, options.esmDestination); - var in_queue = Object.create(null); - var queue = []; - var enqueue = function (filePath) { - if (in_queue[filePath]) { - return; - } - in_queue[filePath] = true; - queue.push(filePath); - }; - var seenDir = {}; - var createDirectoryRecursive = function (dir) { - if (seenDir[dir]) { - return; - } - var lastSlash = dir.lastIndexOf('/'); - if (lastSlash === -1) { - lastSlash = dir.lastIndexOf('\\'); - } - if (lastSlash !== -1) { - createDirectoryRecursive(dir.substring(0, lastSlash)); - } - seenDir[dir] = true; - try { - fs.mkdirSync(dir); - } - catch (err) { } - }; - seenDir[options.repoRoot] = true; - var applyDestinationFolderSimplifications = function (filePath) { - filePath = filePath.replace(/\\/g, '/'); - for (var key in options.destinationFolderSimplification) { - var test = key.replace(/\\/g, '/'); - while (filePath.indexOf(test) >= 0) { - filePath = filePath.replace(test, options.destinationFolderSimplification[test]); - } - } - return filePath; - }; - var shouldSkipImport = function (importText) { - for (var i = 0; i < options.resolveSkip.length; i++) { - var skip = options.resolveSkip[i]; - if (importText.indexOf(skip) === 0) { - return true; - } - } - return false; - }; - var computeDestinationFilePath = function (filePath) { - if (filePath.indexOf(ESM_SRC) === 0) { - // This file is from our sources - return path.join(ESM_DEST, path.relative(ESM_SRC, filePath)); - } - else { - // This file is from node_modules - return path.normalize(applyDestinationFolderSimplifications(path.join(ESM_DEST, path.relative(options.repoRoot, filePath)))); - } - }; - var write = function (filePath, fileContents) { - var finalFilePath = computeDestinationFilePath(filePath); - createDirectoryRecursive(path.dirname(finalFilePath)); - fs.writeFileSync(finalFilePath, fileContents); - }; - options.entryPoints.forEach(function (filePath) { - enqueue(path.join(ESM_SRC, filePath)); - }); - while (queue.length > 0) { - var filePath = queue.shift(); - var fileContents = fs.readFileSync(filePath).toString(); - var info = ts.preProcessFile(fileContents); - for (var i = info.importedFiles.length - 1; i >= 0; i--) { - var importText = info.importedFiles[i].fileName; - if (shouldSkipImport(importText)) { - continue; - } - var pos = info.importedFiles[i].pos; - var end = info.importedFiles[i].end; - if (/(^\.\/)|(^\.\.\/)/.test(importText)) { - // Relative import - var importedFilename = path.join(path.dirname(filePath), importText) + '.js'; - enqueue(importedFilename); - } - else { - var importedFilename = void 0; - if (options.resolveAlias[importText]) { - importedFilename = options.resolveAlias[importText]; - } - else { - importedFilename = findNodeModuleImport(options.repoRoot, importText, filePath); - } - var myDestinationPath = computeDestinationFilePath(filePath); - var importDestinationPath = computeDestinationFilePath(importedFilename); - var relativePath = path.relative(path.dirname(myDestinationPath), importDestinationPath); - if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) { - relativePath = './' + relativePath; - } - relativePath = relativePath.replace(/\\/g, '/'); - relativePath = relativePath.replace(/\.js$/, ''); - fileContents = (fileContents.substring(0, pos + 1) - + relativePath - + fileContents.substring(end + 1)); - enqueue(importedFilename); - } - } - write(filePath, fileContents); - } -} -function findNodeModuleImport(repoRoot, module, sourceFilePath) { - var modulePath = findNodeModule(repoRoot, module, sourceFilePath); - var modulePackagePath = path.join(modulePath, 'package.json'); - if (!fs.existsSync(modulePackagePath)) { - throw new Error("Missing " + modulePackagePath + " in node module " + modulePath); - } - var modulePackage = JSON.parse(fs.readFileSync(modulePackagePath).toString()); - if (typeof modulePackage.module !== 'string') { - throw new Error("Missing property 'module' package.json at " + modulePackagePath); - } - var result = path.join(modulePath, modulePackage.module); - if (!fs.existsSync(result)) { - throw new Error("Missing file " + result); - } - return result; - function findNodeModule(repoRoot, module, sourceFilePath) { - var modulePaths = generatePaths(repoRoot, module, sourceFilePath); - for (var i = 0; i < modulePaths.length; i++) { - if (fs.existsSync(modulePaths[i])) { - return modulePaths[i]; - } - } - throw new Error("Cannot find module " + module + " requested by " + sourceFilePath); - } - function generatePaths(repoRoot, module, sourceFilePath) { - var sourceDir = path.dirname(sourceFilePath); - var result = []; - while (sourceDir.length >= repoRoot.length) { - result.push(path.join(sourceDir, 'node_modules', module)); - sourceDir = path.dirname(sourceDir); - } - return result; - } -} diff --git a/scripts/rmdir.js b/scripts/rmdir.js deleted file mode 100644 index 6bb4855d..00000000 --- a/scripts/rmdir.js +++ /dev/null @@ -1,28 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -const fs = require('fs'); -const path = require('path'); - -const target = path.join(process.cwd(), process.argv[2]); -if (fs.existsSync(target)) { - rmDir(target); -} -console.log(`Deleted ${process.argv[2]}`); - -function rmDir(dirPath) { - let entries = fs.readdirSync(dirPath); - if (entries.length > 0) { - for (var i = 0; i < entries.length; i++) { - var filePath = path.join(dirPath, entries[i]); - if (fs.statSync(filePath).isFile()) { - fs.unlinkSync(filePath); - } else { - rmDir(filePath); - } - } - } - fs.rmdirSync(dirPath); -}