Skip to content

Commit

Permalink
Denoで動くように少し修正 #1654
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Jul 19, 2024
1 parent 3b8c2b8 commit e519eb9
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core
5 changes: 4 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"tasks":{
"build": "deno run --allow-env --allow-read --allow-run bundle_esbuild.ts",
"hello": "deno run --allow-env --allow-read --allow-run src/cnako3.mts hello.nako3"
}
},
"unstable": [
"sloppy-imports"
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nadesiko3",
"version": "3.6.9",
"version": "3.6.10",
"description": "Japanese Programming Language",
"type": "module",
"main": "src/index.mjs",
Expand Down Expand Up @@ -96,7 +96,7 @@
"@eslint/js": "^9.1.1",
"@types/fs-extra": "^11.0.4",
"@types/mocha": "^10.0.6",
"@types/node": "^20.12.7",
"@types/node": "^20.14.11",
"@types/opener": "^1.4.3",
"@types/shell-quote": "^1.7.5",
"babel-loader": "^9.1.3",
Expand Down
50 changes: 25 additions & 25 deletions src/cnako3mod.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// deno-lint-ignore-file no-explicit-any
/**
* コマンドライン版のなでしこ3をモジュールとして定義
* 実際には cnako3.mjs から読み込まれる
Expand All @@ -20,6 +22,8 @@ import fetch from 'node-fetch'

import { NakoGenOptions } from '../core/src/nako_gen.mjs'

import { getEnv, isWindows, getCommandLineArgs, exit } from './deno_wrapper.mjs'

// __dirname のために
import url from 'node:url'
const __filename = url.fileURLToPath(import.meta.url)
Expand Down Expand Up @@ -70,15 +74,7 @@ export class CNako3 extends NakoCompiler {
// CNAKO3で使えるコマンドを登録する
registerCommands () {
// コマンドライン引数を得る
let args: Array<string> = []
if (typeof process !== 'undefined' && process.argv !== undefined) {
args = process.argv
}
else if (typeof (globalThis as any).Deno !== 'undefined') {
args = (globalThis as any).Deno.args
args.unshift('deno')
args.unshift('cnako3.mts')
}
const args: string[] = getCommandLineArgs()
// コマンド引数がないならば、ヘルプを表示(-hはcommandarにデフォルト用意されている)
if (args.length <= 2) { args.push('-h') }

Expand Down Expand Up @@ -213,7 +209,7 @@ export class CNako3 extends NakoCompiler {
} catch (err: any) {
if (this.numFailures > 0) {
this.logger.error(err)
process.exit(1)
exit(1)
}
}
this.outputAST(opt, src)
Expand All @@ -229,7 +225,7 @@ export class CNako3 extends NakoCompiler {
} catch (e: any) {
if (this.numFailures > 0) {
this.logger.error(e)
process.exit(1)
exit(1)
}
}
}
Expand Down Expand Up @@ -268,7 +264,7 @@ export class CNako3 extends NakoCompiler {
const outRuntime = path.join(path.dirname(opt.output), 'nako3runtime')
if (!fs.existsSync(outRuntime)) { fs.mkdirSync(outRuntime) }
// from ./src
for (const mod of ['nako_version.mjs', 'plugin_node.mjs']) {
for (const mod of ['nako_version.mjs', 'plugin_node.mjs', 'deno_wrapper.mjs']) {
fs.copyFileSync(path.join(nakoRuntime, mod), path.join(outRuntime, mod))
}
// from nadesiko3core/src
Expand Down Expand Up @@ -480,7 +476,7 @@ export class CNako3 extends NakoCompiler {
},
readJs: (filePath, token) => {
const loader: any = { task: null }
if (process.platform === 'win32') {
if (isWindows()) {
if (filePath.substring(1, 3) === ':\\') {
filePath = 'file://' + filePath
}
Expand All @@ -492,7 +488,8 @@ export class CNako3 extends NakoCompiler {
loader.task = (
new Promise((resolve, reject) => {
// 一時フォルダを得る
const osTmpDir = (process.platform === 'win32') ? process.env.TEMP : '/tmp'
const tempDir = getEnv('TEMP')
const osTmpDir = isWindows() ? tempDir : '/tmp'
const osTmpDir2 = (osTmpDir) || path.join('./tmp')
const tmpDir = path.join(osTmpDir2, 'com.nadesi.v3.cnako')
const tmpFile = path.join(tmpDir, filePath.replace(/[^a-zA-Z0-9_.]/g, '_'))
Expand Down Expand Up @@ -672,10 +669,11 @@ export class CNako3 extends NakoCompiler {

// 環境変数をチェック
// 環境変数 NAKO_LIB か?
if (process.env.NAKO_LIB) {
const NAKO_LIB = path.join(path.resolve(process.env.NAKO_LIB), pname)
const fileLib = fCheckEx(NAKO_LIB, 'NAKO_LIB')
if (fileLib) { return fileLib }
const nako_lib = getEnv('NAKO_LIB')
if (nako_lib) {
const nako_lib_full = path.join(path.resolve(nako_lib), pname)
const nako_lib_full2 = fCheckEx(nako_lib_full, 'NAKO_LIB')
if (nako_lib_full2) { return nako_lib_full2 }
}

// ランタイムパス/node_modules/<plugin>
Expand All @@ -695,18 +693,20 @@ export class CNako3 extends NakoCompiler {
if (fileRuntimeSrc2) { return fileRuntimeSrc2 }

// 環境変数 NAKO_HOMEか?
if (process.env.NAKO_HOME) {
const NAKO_HOME = path.join(path.resolve(process.env.NAKO_HOME), 'node_modules', pname)
const fileHome = fCheckEx(NAKO_HOME, 'NAKO_HOME')
if (fileHome) { return fileHome }
const nako_home = getEnv('NAKO_HOME')
if (nako_home) {
const nako_home_full = path.join(path.resolve(nako_home), 'node_modules', pname)
const nako_home_full2 = fCheckEx(nako_home_full, 'NAKO_HOME')
if (nako_home_full2) { return nako_home_full2 }
// NAKO_HOME/src ?
const pathNakoHomeSrc = path.join(NAKO_HOME, 'src', pname)
const pathNakoHomeSrc = path.join(nako_home, 'src', pname)
const fileNakoHomeSrc = fCheckEx(pathNakoHomeSrc, 'NAKO_HOME/src')
if (fileNakoHomeSrc) { return fileNakoHomeSrc }
}
// 環境変数 NODE_PATH (global) 以下にあるか?
if (process.env.NODE_PATH) {
const pathNode = path.join(path.resolve(process.env.NODE_PATH), pname)
const node_path = getEnv('NODE_PATH')
if (node_path) {
const pathNode = path.join(path.resolve(node_path), pname)
const fileNode = fCheckEx(pathNode, 'NODE_PATH')
if (fileNode) { return fileNode }
}
Expand Down
50 changes: 50 additions & 0 deletions src/deno_wrapper.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/** get enviroment name */
export function getEnv(name: string): string | undefined {
if (typeof (globalThis as any).process !== 'undefined' && (globalThis as any).process.env !== undefined) {
return (globalThis as any).process.env[name]
}
if (typeof (globalThis as any).Deno !== 'undefined') {
return (globalThis as any).Deno.env.get(name)
}
return undefined
}

/** check is windows */
export function isWindows(): boolean {
if (typeof (globalThis as any).process !== "undefined" && (globalThis as any).process.platform) {
return (globalThis as any).process.platform === "win32"
}
if (typeof (globalThis as any).Deno !== "undefined") {
return (globalThis as any).Deno.build.os === "windows"
}
return false
}

/** get command line arguments */
export function getCommandLineArgs(): string[] {
// Node.js
if (typeof (globalThis as any).process !== "undefined" && (globalThis as any).process.argv) {
return (globalThis as any).process.argv
}
// Deno
if (typeof (globalThis as any).Deno !== "undefined") {
const args = (globalThis as any).Deno.args
args.unshift(new URL((globalThis as any).Deno.mainModule).pathname)
args.unshift((globalThis as any).Deno.execPath())
return args
}
// Node.js環境の場合
return [];
}

/** Exit */
export function exit(code: number): void {
if (typeof (globalThis as any).process !== 'undefined') {
(globalThis as any).process.exit(code)
}
if (typeof (globalThis as any).Deno !== 'undefined') {
(globalThis as any).Deno.exit(code)
}
}
4 changes: 2 additions & 2 deletions src/nako_version.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface NakoVersion {
}
// 実際のバージョン定義 (自動生成されるので以下を編集しない)
const nakoVersion: NakoVersion = {
version: '3.6.9',
version: '3.6.10',
major: 3,
minor: 6,
patch: 9
patch: 10
}
export default nakoVersion
6 changes: 4 additions & 2 deletions src/plugin_node.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import crypto from 'node:crypto'
import os from 'node:os'
import url from 'node:url'
import { NakoSystem } from '../core/src/plugin_api.mjs'
import { getEnv, isWindows, getCommandLineArgs, exit } from './deno_wrapper.mjs'

const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

Expand Down Expand Up @@ -46,7 +48,7 @@ if (typeof (globalThis as any).Deno !== 'undefined') {
nodeProcess = {
platform: (globalThis as any).Deno.build.os,
arch: (globalThis as any).Deno.build.arch,
argv: ['deno', ...(globalThis as any).Deno.args],
argv: getCommandLineArgs(),
exit: (code: number) => {
(globalThis as any).Deno.exit(code)
},
Expand All @@ -73,7 +75,7 @@ export default {
pure: true,
fn: function (sys: NakoSystem) {
// OS判定
const isWin = (nodeProcess.platform === 'win32') || (nodeProcess.platform === 'windows')
const isWin = isWindows()
sys.tags.isWin = isWin
// プラグインの初期化
sys.tags.__quotePath = (fpath: string) => {
Expand Down

0 comments on commit e519eb9

Please sign in to comment.