diff --git a/cli/cli.ts b/cli/cli.ts index 5ba851aea3bf..048cd36f2517 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -2954,17 +2954,17 @@ class SnippetHost implements pxt.Host { if (module.id === "this") { if (filename === "pxt-core.d.ts") { - const contents = fs.readFileSync(path.join(this.getRepoDir(), "libs", "pxt-common", "pxt-core.d.ts"), 'utf8'); + const contents = fs.readFileSync(path.join(this.getPxtRepoDir(), "libs", "pxt-common", "pxt-core.d.ts"), 'utf8'); this.writeFile(module, filename, contents); return contents; } else if (filename === "pxt-helpers.ts") { - const contents = fs.readFileSync(path.resolve(this.getRepoDir(), "libs", "pxt-common", "pxt-helpers.ts"), 'utf8'); + const contents = fs.readFileSync(path.resolve(this.getPxtRepoDir(), "libs", "pxt-common", "pxt-helpers.ts"), 'utf8'); this.writeFile(module, filename, contents); return contents; } else if (filename === "pxt-python.d.ts" || filename === "pxt-python-helpers.ts") { - const contents = fs.readFileSync(path.resolve(this.getRepoDir(), "libs", "pxt-python", filename), 'utf8'); + const contents = fs.readFileSync(path.resolve(this.getPxtRepoDir(), "libs", "pxt-python", filename), 'utf8'); this.writeFile(module, filename, contents); return contents; } @@ -2974,10 +2974,16 @@ class SnippetHost implements pxt.Host { return null; } - private getRepoDir() { + private getPxtRepoDir() { const cwd = process.cwd(); - const i = cwd.lastIndexOf(path.sep + "pxt" + path.sep); - return cwd.substr(0, i + 5); + let p = path.parse(cwd); + while (p.base) { + if (p.base === "pxt") { + return path.format(p); + } + p = path.parse(p.dir); + } + return path.join(cwd, "node_modules", "pxt-core"); } writeFile(module: pxt.Package, filename: string, contents: string) { @@ -4068,6 +4074,7 @@ function compilesOK(opts: pxtc.CompileOptions, fn: string, content: string) { return res.success } + function getApiInfoAsync() { return prepBuildOptionsAsync(BuildOption.GenDocs) .then(opts => { @@ -4874,7 +4881,7 @@ function buildCoreAsync(buildOpts: BuildCoreOptions): Promise { + console.log(`writing ${fileName}`); + const writePath = path.join(builtFolder, fileName); + nodeutil.mkdirP(path.parse(writePath).dir); + fs.writeFileSync(writePath, data); + combined += data + "\n\n"; + } + + // pre-created + for (const file of tsProg.getSourceFiles()) { + if (file.fileName.endsWith(".d.ts")) { + writeDts( + file.fileName, + file.getFullText() + ); + } + } + + // generated via build + tsProg.emit( + /** targetSourceFile **/ undefined, + (fileName: string, data: string) => { + if (!data?.trim()) return; + writeDts(fileName, data); + }, + /** cancellation token **/ undefined, + /** emitOnlyDtsFiles **/ true, + /** customTransformers -> I believe where we should apply culling of deprecated blocks */ + ); + + console.log(`writing combined.d.ts`) + fs.writeFileSync(path.join(builtFolder, "combined.d.ts"), combined); +} + export function gendocsAsync(parsed: commandParser.ParsedCommand) { const docs = !!parsed.flags["docs"]; const locs = !!parsed.flags["locs"]; @@ -6836,6 +6907,12 @@ ${pxt.crowdin.KEY_VARIABLE} - crowdin key }, }, buildShareSimJsAsync) + p.defineCommand({ + name: "buildcoredts", + help: "build d.ts files for core packages", + argString: "", + }, buildCoreDeclarationFiles) + simpleCmd("clean", "removes built folders", cleanAsync); advancedCommand("cleangen", "remove generated files", cleanGenAsync); simpleCmd("npminstallnative", "install native dependencies", npmInstallNativeAsync); diff --git a/localtypings/pxtarget.d.ts b/localtypings/pxtarget.d.ts index aa6b88b1ca8e..0bc6e75ba8d1 100644 --- a/localtypings/pxtarget.d.ts +++ b/localtypings/pxtarget.d.ts @@ -1076,6 +1076,7 @@ declare namespace ts.pxtc { clearIncrBuildAndRetryOnError?: boolean; // on error when compiling in service, try again with a full recompile. errorOnGreyBlocks?: boolean; generateSourceMap?: boolean; + tsCompileOptions?: pxt.Map; // ts.CompilerOptions; otherMultiVariants?: ExtensionTarget[]; diff --git a/pxtcompiler/emitter/driver.ts b/pxtcompiler/emitter/driver.ts index b9c11e4e5f13..16f356aa8363 100644 --- a/pxtcompiler/emitter/driver.ts +++ b/pxtcompiler/emitter/driver.ts @@ -25,6 +25,12 @@ namespace ts.pxtc { options.noImplicitAny = true; options.noImplicitReturns = true; options.allowUnreachableCode = true; + options.declaration = true; + + for (const [key, value] of Object.entries(opts?.tsCompileOptions ?? {})) { + options[key] = value; + } + return options }