Skip to content

Commit

Permalink
fix(bindgen): write symlink on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Feb 29, 2024
1 parent c0ab33a commit f74e39c
Showing 1 changed file with 176 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,131 @@
import fs from 'fs-extra'
import fs from 'fs'
import { execSync } from 'child_process'
import os from 'os'
import path from 'path'

import writeIfOverrideNotPresent from '../write-if-override-not-present.js'

function writeSupportFiles(outputDir, forNode, bindgenResource, packageName, packageDescription) {
function writeSupportFiles(
outputDir,
forNode,
bindgenResource,
packageName,
packageDescription
) {
if (!forNode) {
try {
fs.mkdirSync(path.join(outputDir, 'build'), { recursive: true })
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
try {
fs.mkdirSync(path.join(outputDir, 'test', 'browser', 'demo-app'), { recursive: true })
fs.mkdirSync(path.join(outputDir, 'test', 'browser', 'demo-app'), {
recursive: true
})
} catch (err) {
if (err.code !== 'EEXIST') throw err
}

const pipelinesBaseUrlPath = path.join(outputDir, 'src', 'pipelines-base-url.ts')
const pipelinesBaseUrlPath = path.join(
outputDir,
'src',
'pipelines-base-url.ts'
)
if (!fs.existsSync(pipelinesBaseUrlPath)) {
fs.copyFileSync(bindgenResource('pipelines-base-url.ts'), pipelinesBaseUrlPath)
let pipelinesBaseUrlPathContent = fs.readFileSync(bindgenResource('pipelines-base-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelinesBaseUrlPathContent = pipelinesBaseUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
writeIfOverrideNotPresent(pipelinesBaseUrlPath, pipelinesBaseUrlPathContent)
fs.copyFileSync(
bindgenResource('pipelines-base-url.ts'),
pipelinesBaseUrlPath
)
let pipelinesBaseUrlPathContent = fs.readFileSync(
bindgenResource('pipelines-base-url.ts'),
{ encoding: 'utf8', flag: 'r' }
)
pipelinesBaseUrlPathContent = pipelinesBaseUrlPathContent.replaceAll(
'<bindgenPackageName>',
packageName
)
writeIfOverrideNotPresent(
pipelinesBaseUrlPath,
pipelinesBaseUrlPathContent
)
}
const pipelineWorkerUrlPath = path.join(outputDir, 'src', 'pipeline-worker-url.ts')
const pipelineWorkerUrlPath = path.join(
outputDir,
'src',
'pipeline-worker-url.ts'
)
if (!fs.existsSync(pipelineWorkerUrlPath)) {
let pipelineWorkerUrlPathContent = fs.readFileSync(bindgenResource('pipeline-worker-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelineWorkerUrlPathContent = pipelineWorkerUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
writeIfOverrideNotPresent(pipelineWorkerUrlPath, pipelineWorkerUrlPathContent)
let pipelineWorkerUrlPathContent = fs.readFileSync(
bindgenResource('pipeline-worker-url.ts'),
{ encoding: 'utf8', flag: 'r' }
)
pipelineWorkerUrlPathContent = pipelineWorkerUrlPathContent.replaceAll(
'<bindgenPackageName>',
packageName
)
writeIfOverrideNotPresent(
pipelineWorkerUrlPath,
pipelineWorkerUrlPathContent
)
}
const defaultWebWorkerPath = path.join(outputDir, 'src', 'default-web-worker.ts')
const defaultWebWorkerPath = path.join(
outputDir,
'src',
'default-web-worker.ts'
)
if (!fs.existsSync(defaultWebWorkerPath)) {
let defaultWebWorkerContent = fs.readFileSync(bindgenResource('default-web-worker.ts'), { encoding: 'utf8', flag: 'r' })
let defaultWebWorkerContent = fs.readFileSync(
bindgenResource('default-web-worker.ts'),
{ encoding: 'utf8', flag: 'r' }
)
writeIfOverrideNotPresent(defaultWebWorkerPath, defaultWebWorkerContent)
}

const indexWorkerEmbeddedPath = path.join(outputDir, 'src', 'index-worker-embedded.ts')
const indexWorkerEmbeddedContent = fs.readFileSync(bindgenResource('index-worker-embedded.ts'), { encoding: 'utf8', flag: 'r' })
writeIfOverrideNotPresent(indexWorkerEmbeddedPath, indexWorkerEmbeddedContent)
const indexWorkerEmbeddedMinPath = path.join(outputDir, 'src', 'index-worker-embedded.min.ts')
const indexWorkerEmbeddedMinContent = fs.readFileSync(bindgenResource('index-worker-embedded.min.ts'), { encoding: 'utf8', flag: 'r' })
writeIfOverrideNotPresent(indexWorkerEmbeddedMinPath, indexWorkerEmbeddedMinContent)
const indexWorkerEmbeddedPath = path.join(
outputDir,
'src',
'index-worker-embedded.ts'
)
const indexWorkerEmbeddedContent = fs.readFileSync(
bindgenResource('index-worker-embedded.ts'),
{ encoding: 'utf8', flag: 'r' }
)
writeIfOverrideNotPresent(
indexWorkerEmbeddedPath,
indexWorkerEmbeddedContent
)
const indexWorkerEmbeddedMinPath = path.join(
outputDir,
'src',
'index-worker-embedded.min.ts'
)
const indexWorkerEmbeddedMinContent = fs.readFileSync(
bindgenResource('index-worker-embedded.min.ts'),
{ encoding: 'utf8', flag: 'r' }
)
writeIfOverrideNotPresent(
indexWorkerEmbeddedMinPath,
indexWorkerEmbeddedMinContent
)

const packageJsonSymlinkPath = path.join(outputDir, 'src', 'package.json')
if (!fs.existsSync(packageJsonSymlinkPath)) {
fs.symlinkSync('../package.json', packageJsonSymlinkPath)
if (os.platform() === 'win32') {
const outputPath = path.join('..', 'package.json')
const previousCwd = process.cwd()
process.chdir(path.join(outputDir, 'src'))
if (
typeof fs.lstatSync('package.json', { throwIfNoEntry: false }) ===
'undefined'
) {
execSync(`mklink /J package.json ${outputPath}`, {
windowsHide: true
})
}
process.chdir(previousCwd)
} else {
fs.symlinkSync(path.join('..', 'package.json'), packageJsonSymlinkPath)
}
}

const npmIgnorePath = path.join(outputDir, '.npmignore')
Expand All @@ -54,29 +135,89 @@ function writeSupportFiles(outputDir, forNode, bindgenResource, packageName, pac

const docsIndexPath = path.join(outputDir, 'index.html')
if (!fs.existsSync(docsIndexPath)) {
let docsIndexContent = fs.readFileSync(bindgenResource('index.html'), { encoding: 'utf8', flag: 'r' })
docsIndexContent = docsIndexContent.replaceAll('<bindgenPackageName>', packageName)
docsIndexContent = docsIndexContent.replaceAll('<bindgenPackageDescription>', packageDescription)
let docsIndexContent = fs.readFileSync(bindgenResource('index.html'), {
encoding: 'utf8',
flag: 'r'
})
docsIndexContent = docsIndexContent.replaceAll(
'<bindgenPackageName>',
packageName
)
docsIndexContent = docsIndexContent.replaceAll(
'<bindgenPackageDescription>',
packageDescription
)
fs.writeFileSync(docsIndexPath, docsIndexContent)
fs.copyFileSync(bindgenResource('.nojekyll'), path.join(outputDir, '.nojekll'))
fs.copyFileSync(
bindgenResource('.nojekyll'),
path.join(outputDir, '.nojekll')
)
}

const logoPath = path.join(outputDir, 'test', 'browser', 'demo-app', 'logo.svg')
const logoPath = path.join(
outputDir,
'test',
'browser',
'demo-app',
'logo.svg'
)
if (!fs.existsSync(logoPath)) {
fs.copyFileSync(bindgenResource(path.join('demo-app', 'logo.svg')), logoPath)
const jsLogoPath = path.join(outputDir, 'test', 'browser', 'demo-app', 'javascript-logo.svg')
fs.copyFileSync(bindgenResource(path.join('demo-app', 'javascript-logo.svg')), jsLogoPath)
const tsLogoPath = path.join(outputDir, 'test', 'browser', 'demo-app', 'typescript-logo.svg')
fs.copyFileSync(bindgenResource(path.join('demo-app', 'typescript-logo.svg')), tsLogoPath)
fs.copyFileSync(
bindgenResource(path.join('demo-app', 'logo.svg')),
logoPath
)
const jsLogoPath = path.join(
outputDir,
'test',
'browser',
'demo-app',
'javascript-logo.svg'
)
fs.copyFileSync(
bindgenResource(path.join('demo-app', 'javascript-logo.svg')),
jsLogoPath
)
const tsLogoPath = path.join(
outputDir,
'test',
'browser',
'demo-app',
'typescript-logo.svg'
)
fs.copyFileSync(
bindgenResource(path.join('demo-app', 'typescript-logo.svg')),
tsLogoPath
)
}

const demoStylePath = path.join(outputDir, 'test', 'browser', 'demo-app', 'style.css')
const demoStylePath = path.join(
outputDir,
'test',
'browser',
'demo-app',
'style.css'
)
if (!fs.existsSync(demoStylePath)) {
fs.copyFileSync(bindgenResource(path.join('demo-app', 'style.css')), demoStylePath)
fs.copyFileSync(
bindgenResource(path.join('demo-app', 'style.css')),
demoStylePath
)
}

const demoJsUtilities = path.join(outputDir, 'test', 'browser', 'demo-app', 'utilities.js')
writeIfOverrideNotPresent(demoJsUtilities, fs.readFileSync(bindgenResource(path.join('demo-app', 'utilities.js')), { encoding: 'utf8', flag: 'r' }))
const demoJsUtilities = path.join(
outputDir,
'test',
'browser',
'demo-app',
'utilities.js'
)
writeIfOverrideNotPresent(
demoJsUtilities,
fs.readFileSync(bindgenResource(path.join('demo-app', 'utilities.js')), {
encoding: 'utf8',
flag: 'r'
})
)

const viteConfigPath = path.join(outputDir, 'vite.config.js')
if (!fs.existsSync(viteConfigPath)) {
Expand Down

0 comments on commit f74e39c

Please sign in to comment.