diff --git a/pkg/codegen/nodejs/gen.go b/pkg/codegen/nodejs/gen.go index be726aca09c7..5f434c3ba491 100644 --- a/pkg/codegen/nodejs/gen.go +++ b/pkg/codegen/nodejs/gen.go @@ -2591,9 +2591,9 @@ func genTypeScriptProjectFile(info NodePackageInfo, files codegen.Fs) string { "experimentalDecorators": true, "noFallthroughCasesInSwitch": true, "forceConsistentCasingInFileNames": true, - "paths": { + "paths": { "@/*": ["*"] - }, + }, "strict": true }, "files": [ diff --git a/pkg/codegen/testing/test/sdk_driver.go b/pkg/codegen/testing/test/sdk_driver.go index a105126bb887..259cd3e4d5b9 100644 --- a/pkg/codegen/testing/test/sdk_driver.go +++ b/pkg/codegen/testing/test/sdk_driver.go @@ -164,6 +164,12 @@ var PulumiPulumiSDKTests = []*SDKTest{ Description: "A resource with the same name as its property", SkipCompileCheck: codegen.NewStringSet(dotnet, nodejs), }, + { + Directory: "type-references-resource", + Description: "An instance where a type references a resource", + Skip: allLanguages.Except("nodejs/any"), + // SkipCompileCheck: codegen.NewStringSet(dotnet, golang, python), + }, { Directory: "hyphen-url", Description: "A resource url with a hyphen in its path", diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/README.md b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/README.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/codegen-manifest.json b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/codegen-manifest.json new file mode 100644 index 000000000000..b4e5513101a2 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/codegen-manifest.json @@ -0,0 +1,19 @@ +{ + "emittedFiles": [ + "README.md", + "index.ts", + "my_mod/index.ts", + "my_mod/triceratops.ts", + "package.json", + "provider.ts", + "scripts/install-pulumi-plugin.js", + "tsconfig.json", + "types/index.ts", + "types/input.ts", + "types/my_mod/index.ts", + "types/my_mod/input.ts", + "types/my_mod/output.ts", + "types/output.ts", + "utilities.ts" + ] +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/index.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/index.ts new file mode 100644 index 000000000000..49de88cb48a9 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/index.ts @@ -0,0 +1,30 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as utilities from "./utilities"; + +// Export members: +export { ProviderArgs } from "./provider"; +export type Provider = import("./provider").Provider; +export const Provider: typeof import("./provider").Provider = null as any; +utilities.lazyLoad(exports, ["Provider"], () => require("./provider")); + + +// Export sub-modules: +import * as my_mod from "./my_mod"; +import * as types from "./types"; + +export { + my_mod, + types, +}; +pulumi.runtime.registerResourcePackage("dinosaurs", { + version: utilities.getVersion(), + constructProvider: (name: string, type: string, urn: string): pulumi.ProviderResource => { + if (type !== "pulumi:providers:dinosaurs") { + throw new Error(`unknown provider type ${type}`); + } + return new Provider(name, undefined, { urn }); + }, +}); diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/index.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/index.ts new file mode 100644 index 000000000000..1af01116c275 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/index.ts @@ -0,0 +1,25 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as utilities from "../utilities"; + +// Export members: +export { TriceratopsArgs } from "./triceratops"; +export type Triceratops = import("./triceratops").Triceratops; +export const Triceratops: typeof import("./triceratops").Triceratops = null as any; +utilities.lazyLoad(exports, ["Triceratops"], () => require("./triceratops")); + + +const _module = { + version: utilities.getVersion(), + construct: (name: string, type: string, urn: string): pulumi.Resource => { + switch (type) { + case "dinosaurs:my_mod:Triceratops": + return new Triceratops(name, undefined, { urn }) + default: + throw new Error(`unknown resource type ${type}`); + } + }, +}; +pulumi.runtime.registerResourceModule("dinosaurs", "my_mod", _module) diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/triceratops.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/triceratops.ts new file mode 100644 index 000000000000..2f889b43dfb0 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/my_mod/triceratops.ts @@ -0,0 +1,63 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as utilities from "../utilities"; + +/** + * My favorite dino + */ +export class Triceratops extends pulumi.CustomResource { + /** + * Get an existing Triceratops resource's state with the given name, ID, and optional extra + * properties used to qualify the lookup. + * + * @param name The _unique_ name of the resulting resource. + * @param id The _unique_ provider ID of the resource to lookup. + * @param opts Optional settings to control the behavior of the CustomResource. + */ + public static get(name: string, id: pulumi.Input, opts?: pulumi.CustomResourceOptions): Triceratops { + return new Triceratops(name, undefined as any, { ...opts, id: id }); + } + + /** @internal */ + public static readonly __pulumiType = 'dinosaurs:my_mod:Triceratops'; + + /** + * Returns true if the given object is an instance of Triceratops. This is designed to work even + * when multiple copies of the Pulumi SDK have been loaded into the same process. + */ + public static isInstance(obj: any): obj is Triceratops { + if (obj === undefined || obj === null) { + return false; + } + return obj['__pulumiType'] === Triceratops.__pulumiType; + } + + public /*out*/ readonly size!: pulumi.Output; + + /** + * Create a Triceratops resource with the given unique name, arguments, and options. + * + * @param name The _unique_ name of the resource. + * @param args The arguments to use to populate this resource's properties. + * @param opts A bag of options that control this resource's behavior. + */ + constructor(name: string, args?: TriceratopsArgs, opts?: pulumi.CustomResourceOptions) { + let resourceInputs: pulumi.Inputs = {}; + opts = opts || {}; + if (!opts.id) { + resourceInputs["size"] = undefined /*out*/; + } else { + resourceInputs["size"] = undefined /*out*/; + } + opts = pulumi.mergeOptions(utilities.resourceOptsDefaults(), opts); + super(Triceratops.__pulumiType, name, resourceInputs, opts); + } +} + +/** + * The set of arguments for constructing a Triceratops resource. + */ +export interface TriceratopsArgs { +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/package.json b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/package.json new file mode 100644 index 000000000000..80cb461cb338 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/package.json @@ -0,0 +1,18 @@ +{ + "name": "@pulumi/dinosaurs", + "version": "${VERSION}", + "scripts": { + "build": "tsc", + "install": "node scripts/install-pulumi-plugin.js resource dinosaurs ${VERSION}" + }, + "dependencies": { + "@pulumi/pulumi": "^3.12" + }, + "devDependencies": { + "typescript": "^3.7.0" + }, + "pulumi": { + "resource": true, + "name": "dinosaurs" + } +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/provider.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/provider.ts new file mode 100644 index 000000000000..8ec4b88027a6 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/provider.ts @@ -0,0 +1,44 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as utilities from "./utilities"; + +export class Provider extends pulumi.ProviderResource { + /** @internal */ + public static readonly __pulumiType = 'dinosaurs'; + + /** + * Returns true if the given object is an instance of Provider. This is designed to work even + * when multiple copies of the Pulumi SDK have been loaded into the same process. + */ + public static isInstance(obj: any): obj is Provider { + if (obj === undefined || obj === null) { + return false; + } + return obj['__pulumiType'] === Provider.__pulumiType; + } + + + /** + * Create a Provider resource with the given unique name, arguments, and options. + * + * @param name The _unique_ name of the resource. + * @param args The arguments to use to populate this resource's properties. + * @param opts A bag of options that control this resource's behavior. + */ + constructor(name: string, args?: ProviderArgs, opts?: pulumi.ResourceOptions) { + let resourceInputs: pulumi.Inputs = {}; + opts = opts || {}; + { + } + opts = pulumi.mergeOptions(utilities.resourceOptsDefaults(), opts); + super(Provider.__pulumiType, name, resourceInputs, opts); + } +} + +/** + * The set of arguments for constructing a Provider resource. + */ +export interface ProviderArgs { +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/scripts/install-pulumi-plugin.js b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/scripts/install-pulumi-plugin.js new file mode 100644 index 000000000000..fefc6e0eb097 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/scripts/install-pulumi-plugin.js @@ -0,0 +1,26 @@ +"use strict"; +var childProcess = require("child_process"); + +var args = process.argv.slice(2); + +if (args.indexOf("${VERSION}") !== -1) { + process.exit(0); +} + +var res = childProcess.spawnSync("pulumi", ["plugin", "install"].concat(args), { + stdio: ["ignore", "inherit", "inherit"] +}); + +if (res.error && res.error.code === "ENOENT") { + console.error("\nThere was an error installing the resource provider plugin. " + + "It looks like `pulumi` is not installed on your system. " + + "Please visit https://pulumi.com/ to install the Pulumi CLI.\n" + + "You may try manually installing the plugin by running " + + "`pulumi plugin install " + args.join(" ") + "`"); +} else if (res.error || res.status !== 0) { + console.error("\nThere was an error installing the resource provider plugin. " + + "You may try to manually installing the plugin by running " + + "`pulumi plugin install " + args.join(" ") + "`"); +} + +process.exit(0); diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/tsconfig.json b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/tsconfig.json new file mode 100644 index 000000000000..ee2af7b52fa8 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "outDir": "bin", + "target": "es2016", + "module": "commonjs", + "moduleResolution": "node", + "declaration": true, + "sourceMap": true, + "stripInternal": true, + "experimentalDecorators": true, + "noFallthroughCasesInSwitch": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@/*": ["*"] + }, + "strict": true + }, + "files": [ + "index.ts", + "my_mod/index.ts", + "my_mod/triceratops.ts", + "provider.ts", + "types/index.ts", + "types/input.ts", + "types/my_mod/index.ts", + "types/my_mod/input.ts", + "types/my_mod/output.ts", + "types/output.ts", + "utilities.ts" + ] +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/index.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/index.ts new file mode 100644 index 000000000000..d1f303f60379 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/index.ts @@ -0,0 +1,13 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as utilities from "../utilities"; + +// Export sub-modules: +import * as input from "./input"; +import * as output from "./output"; + +export { + input, + output, +}; diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/input.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/input.ts new file mode 100644 index 000000000000..48d0c42e5692 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/input.ts @@ -0,0 +1,11 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as inputs from "../types/input"; +import * as outputs from "../types/output"; +import * as utilities from "../utilities"; + +import {Triceratops} from "@/my_mod"; + +export * as my_mod from "./my_mod/input"; diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/index.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/index.ts new file mode 100644 index 000000000000..b7b2ea440d9f --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/index.ts @@ -0,0 +1,3 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/input.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/input.ts new file mode 100644 index 000000000000..79d8d682639c --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/input.ts @@ -0,0 +1,10 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as inputs from "../../types/input"; +import * as outputs from "../../types/output"; +import * as utilities from "../../utilities"; + +import {Triceratops} from "@/my_mod"; + diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/output.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/output.ts new file mode 100644 index 000000000000..79d8d682639c --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/my_mod/output.ts @@ -0,0 +1,10 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as inputs from "../../types/input"; +import * as outputs from "../../types/output"; +import * as utilities from "../../utilities"; + +import {Triceratops} from "@/my_mod"; + diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/output.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/output.ts new file mode 100644 index 000000000000..e1a0a5d08f58 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/types/output.ts @@ -0,0 +1,11 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +import * as pulumi from "@pulumi/pulumi"; +import * as inputs from "../types/input"; +import * as outputs from "../types/output"; +import * as utilities from "../utilities"; + +import {Triceratops} from "@/my_mod"; + +export * as my_mod from "./my_mod/output"; diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/utilities.ts b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/utilities.ts new file mode 100644 index 000000000000..fae1054b6bab --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/nodejs/utilities.ts @@ -0,0 +1,66 @@ +// *** WARNING: this file was generated by test. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + + +export function getEnv(...vars: string[]): string | undefined { + for (const v of vars) { + const value = process.env[v]; + if (value) { + return value; + } + } + return undefined; +} + +export function getEnvBoolean(...vars: string[]): boolean | undefined { + const s = getEnv(...vars); + if (s !== undefined) { + // NOTE: these values are taken from https://golang.org/src/strconv/atob.go?s=351:391#L1, which is what + // Terraform uses internally when parsing boolean values. + if (["1", "t", "T", "true", "TRUE", "True"].find(v => v === s) !== undefined) { + return true; + } + if (["0", "f", "F", "false", "FALSE", "False"].find(v => v === s) !== undefined) { + return false; + } + } + return undefined; +} + +export function getEnvNumber(...vars: string[]): number | undefined { + const s = getEnv(...vars); + if (s !== undefined) { + const f = parseFloat(s); + if (!isNaN(f)) { + return f; + } + } + return undefined; +} + +export function getVersion(): string { + let version = require('./package.json').version; + // Node allows for the version to be prefixed by a "v", while semver doesn't. + // If there is a v, strip it off. + if (version.indexOf('v') === 0) { + version = version.slice(1); + } + return version; +} + +/** @internal */ +export function resourceOptsDefaults(): any { + return { version: getVersion() }; +} + +/** @internal */ +export function lazyLoad(exports: any, props: string[], loadModule: any) { + for (let property of props) { + Object.defineProperty(exports, property, { + enumerable: true, + get: function() { + return loadModule()[property]; + }, + }); + } +} diff --git a/pkg/codegen/testing/test/testdata/type-references-resource/schema.json b/pkg/codegen/testing/test/testdata/type-references-resource/schema.json new file mode 100644 index 000000000000..546954669410 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/type-references-resource/schema.json @@ -0,0 +1,47 @@ +{ + "version": "0.0.1", + "name": "dinosaurs", + "types": { + "dinosaurs:my_mod:BigDino": { + "description": "A big dinosaur", + "properties": { + "kind": { + "$ref": "#/types/dinosaurs:my_mod:Triceratops" + } + }, + "type": "object" + } + }, + "resources": { + "dinosaurs:my_mod:Triceratops": { + "description": "My favorite dino", + "properties": { + "size": { + "type": "integer" + } + }, + "type": "object" + } + }, + "functions": {}, + "language": { + "csharp": { + "projectReferences": [ + "..\\..\\..\\..\\..\\..\\..\\sdk\\dotnet\\Pulumi\\Pulumi.csproj" + ] + }, + "go": { + "importBasePath": "github.com/pulumi/pulumi/pkg/v3/codegen/testing/test/testdata/type-references-resource/go/example", + "generateExtraInputTypes": true + }, + "nodejs": { + "dependencies": { + "@pulumi/pulumi": "^3.12" + }, + "devDependencies": { + "typescript": "^3.7.0" + } + }, + "python": {} + } +}