Skip to content

Commit

Permalink
Merge pull request #48 from Coder-Spirit/fix-symbols-import-on-deno
Browse files Browse the repository at this point in the history
fix: suport nominal-symbols in deno "pkg"
  • Loading branch information
castarco authored Jan 22, 2022
2 parents 1f69194 + dd0614c commit dfdd610
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
22 changes: 11 additions & 11 deletions nominal/deno/internal/Symbols.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const __BaseType: unique symbol = Symbol('__BaseType')
export const __Properties: unique symbol = Symbol('__Properties')
export const __Brand: unique symbol = Symbol('__Brand')

export const __PropertyName: unique symbol = Symbol('__PropertyName')
export const __PropertyValues: unique symbol = Symbol('__PropertyValues')

// Some Nominal tricks rely on the fact that this symbol and its type won't be
// exported as public to the library's users.
const __ImpossibleSymbol: unique symbol = Symbol('__Impossible')
export type __Impossible = typeof __ImpossibleSymbol
// We re-export, instead of directly importing from the package, for
// compatibility reasons. Typescript and NodeJS are still in very
// early stages when it comes to ES modules support.
export {
__BaseType,
__Properties,
__Brand,
__PropertyName,
__PropertyValues,
type __Impossible
} from 'https://deno.land/x/[email protected]/nominal-symbols/deno/index.ts'
6 changes: 5 additions & 1 deletion nominal/scripts/adaptDeno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ run()

async function run(): Promise<void> {
const esmDir = join(__dirname, '..', 'deno')
await processDirectory(esmDir, '.ts')
await processDirectory(
esmDir,
'.ts',
[['@coderspirit/nominal-symbols', 'https://deno.land/x/[email protected]/nominal-symbols/deno/index.ts']]
)
}
3 changes: 1 addition & 2 deletions nominal/scripts/build_deno.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
mkdir -p ./deno \
&& cp -r ./src/* ./deno/ \
&& rm -rf ./deno/__tests__ \
&& rm ./deno/internal/Symbols.ts \
&& mv ./deno/internal/Symbols.ts.esm ./deno/internal/Symbols.ts \
&& rm ./deno/internal/Symbols.ts.esm \
&& ts-node ./scripts/adaptDeno.ts
15 changes: 12 additions & 3 deletions nominal/scripts/lib/processFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join } from 'path'
export async function processDirectory(
directoryPath: string,
ext: string,
exactReplacements: ([string, string])[] = []
): Promise<void> {
const fileNames = await readdir(directoryPath)

Expand All @@ -13,16 +14,17 @@ export async function processDirectory(
const fileStats = await lstat(filePath)

if (fileStats.isDirectory()) {
await processDirectory(filePath, ext)
await processDirectory(filePath, ext, exactReplacements)
} else {
await processFile(filePath, ext)
await processFile(filePath, ext, exactReplacements)
}
}
}

export async function processFile(
filePath: string,
ext: string,
exactReplacements: ([string, string])[] = []
): Promise<void> {
const thePattern = /^(.*}\s+from\s+'\.[A-Za-z0-9_./]+)';?\n?$/s
const hasExtension = /\.(js|ts|d\.ts)$/s
Expand All @@ -42,7 +44,14 @@ export async function processFile(
transformedLines.push(`${theMatch[1] as string}${ext}';`)
transformed = true
} else {
transformedLines.push(line)
let replaced = line
for (const [pattern, replacement] of exactReplacements) {
if (replaced.includes(pattern)) {
replaced = replaced.replace(pattern, replacement)
transformed = true
}
}
transformedLines.push(replaced)
}
}

Expand Down
2 changes: 1 addition & 1 deletion nominal/src/internal/Symbols.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// We re-export, instead of directly importing from the package, for
// compatibility with ES modules & Deno. Typescript and NodeJS are still in very
// compatibility reasons. Typescript and NodeJS are still in very
// early stages when it comes to ES modules support.
export {
__BaseType,
Expand Down

0 comments on commit dfdd610

Please sign in to comment.