|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import { env } from 'process'; |
| 5 | +import { ChildProcessByStdio, spawn } from 'child_process'; |
| 6 | +import { Readable } from 'stream'; |
| 7 | + |
| 8 | +const findNbcode = (extensionPath: string): string => { |
| 9 | + let nbcode = os.platform() === 'win32' ? |
| 10 | + os.arch() === 'x64' ? 'nbcode64.exe' : 'nbcode.exe' |
| 11 | + : 'nbcode.sh'; |
| 12 | + let nbcodePath = path.join(extensionPath, "nbcode", "bin", nbcode); |
| 13 | + |
| 14 | + let nbcodePerm = fs.statSync(nbcodePath); |
| 15 | + if (!nbcodePerm.isFile()) { |
| 16 | + throw `Cannot execute ${nbcodePath}`; |
| 17 | + } |
| 18 | + if (os.platform() !== 'win32') { |
| 19 | + fs.chmodSync(path.join(extensionPath, "nbcode", "bin", nbcode), "744"); |
| 20 | + fs.chmodSync(path.join(extensionPath, "nbcode", "platform", "lib", "nbexec.sh"), "744"); |
| 21 | + fs.chmodSync(path.join(extensionPath, "nbcode", "java", "maven", "bin", "mvn.sh"), "744"); |
| 22 | + } |
| 23 | + return nbcodePath; |
| 24 | +} |
| 25 | + |
| 26 | +if (typeof process === 'object' && process.argv0 === 'node') { |
| 27 | + let extension = path.join(process.argv[1], '..', '..', '..'); |
| 28 | + let nbcode = path.join(extension, 'nbcode'); |
| 29 | + if (!fs.existsSync(nbcode)) { |
| 30 | + throw `Cannot find ${nbcode}. Try npm run compile first!`; |
| 31 | + } |
| 32 | + let clusters = fs.readdirSync(nbcode).filter(c => c !== 'bin' && c !== 'etc').map(c => path.join(nbcode, c)); |
| 33 | + let args = process.argv.slice(2); |
| 34 | + let json = JSON.parse("" + fs.readFileSync(path.join(extension, 'package.json'))); |
| 35 | + let storage; |
| 36 | + |
| 37 | + if (!env.nbcode_userdir || env.nbcode_userdir == 'global') { |
| 38 | + storage = path.join(os.platform() === 'darwin' ? |
| 39 | + path.join(os.homedir(), 'Library', 'Application Support') : |
| 40 | + path.join(os.homedir(), '.config'), |
| 41 | + 'Code', 'User', 'globalStorage', json.publisher + '.' + json.name); |
| 42 | + } else { |
| 43 | + storage = env.nbcode_userdir; |
| 44 | + } |
| 45 | + const userdir = path.join(storage, "userdir"); |
| 46 | + |
| 47 | + if (!fs.existsSync(userdir)) { |
| 48 | + fs.mkdirSync(userdir, { recursive: true }); |
| 49 | + const stats = fs.statSync(userdir); |
| 50 | + if (!stats.isDirectory()) { |
| 51 | + throw `${userdir} is not a directory`; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + console.log('Launching NBLS with user directory: ' + userdir); |
| 56 | + const ideArgs = []; |
| 57 | + ideArgs.push(`-J-Dnetbeans.extra.dirs="${clusters.join(path.delimiter)}"`, ...args); |
| 58 | + const nbcodeBinPath = findNbcode(extension); |
| 59 | + const nbProcess: ChildProcessByStdio<any, Readable, Readable> = spawn(nbcodeBinPath, ideArgs, { |
| 60 | + cwd: userdir, |
| 61 | + stdio: ["ignore", "pipe", "pipe"], |
| 62 | + }); |
| 63 | + |
| 64 | + nbProcess.stdout.on('data', function (data) { |
| 65 | + console.log(data.toString()); |
| 66 | + }); |
| 67 | + nbProcess.stderr.on('data', function (data) { |
| 68 | + console.log(data.toString()); |
| 69 | + }); |
| 70 | + nbProcess.on('close', (code) => { |
| 71 | + console.log(`nbcode finished with status ${code}`); |
| 72 | + }); |
| 73 | +} |
0 commit comments