Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Luau package template #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
type: [game, place, model, plugin, package]
type: [game, place, model, plugin, package, luau-package]
steps:
- name: Checkout create-roblox-ts repo
uses: actions/checkout@v4
Expand Down
60 changes: 46 additions & 14 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum InitMode {
Model = "model",
Plugin = "plugin",
Package = "package",
LuauPackage = "luau-package",
}

enum PackageManager {
Expand Down Expand Up @@ -79,6 +80,14 @@ function cmd(cmdStr: string, cwd: string) {
});
}

function shouldHaveDefaultScripts(template: InitMode) {
return template !== InitMode.LuauPackage;
}

function isPackageTemplate(template: InitMode): template is InitMode.Package | InitMode.LuauPackage {
return template === InitMode.Package || template === InitMode.LuauPackage;
}

const GIT_IGNORE = ["/node_modules", "/out", "/include", "*.tsbuildinfo"];

async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {
Expand Down Expand Up @@ -141,10 +150,12 @@ async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {
type: () => initMode === InitMode.None && "select",
name: "template",
message: "Select template",
choices: [InitMode.Game, InitMode.Model, InitMode.Plugin, InitMode.Package].map(value => ({
title: value,
value,
})),
choices: [InitMode.Game, InitMode.Model, InitMode.Plugin, InitMode.Package, InitMode.LuauPackage].map(
LouieK22 marked this conversation as resolved.
Show resolved Hide resolved
value => ({
title: value,
value,
}),
),
initial: 0,
},
{
Expand Down Expand Up @@ -225,18 +236,32 @@ async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {
await benchmark("Initializing package.json..", async () => {
await cmd(selectedPackageManager.init, cwd);
const pkgJson = await fs.readJson(paths.packageJson);
pkgJson.scripts = {
build: "rbxtsc",
watch: "rbxtsc -w",
};
if (template === InitMode.Package) {

if (shouldHaveDefaultScripts(template)) {
pkgJson.scripts = {
build: "rbxtsc",
watch: "rbxtsc -w",
};
} else {
pkgJson.scripts = undefined;
}

if (isPackageTemplate(template)) {
pkgJson.name = RBXTS_SCOPE + "/" + pkgJson.name;
pkgJson.publishConfig = { access: "public" };
}

if (template === InitMode.LuauPackage) {
pkgJson.main = "src/init.lua";
pkgJson.types = "src/index.d.ts";
pkgJson.files = ["src"];
} else if (template === InitMode.Package) {
pkgJson.main = "out/init.lua";
pkgJson.types = "out/index.d.ts";
pkgJson.files = ["out", "!**/*.tsbuildinfo"];
pkgJson.publishConfig = { access: "public" };
pkgJson.scripts.prepublishOnly = selectedPackageManager.build;
}

await fs.outputFile(paths.packageJson, JSON.stringify(pkgJson, null, 2));
});

Expand All @@ -256,12 +281,15 @@ async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {

await benchmark("Installing dependencies..", async () => {
const devDependencies = [
"roblox-ts" + (compilerVersion ? `@${compilerVersion}` : ""),
"@rbxts/compiler-types" + (compilerVersion ? `@compiler-${compilerVersion}` : ""),
"@rbxts/types",
"typescript",
];

if (template !== InitMode.LuauPackage) {
devDependencies.push("roblox-ts" + (compilerVersion ? `@${compilerVersion}` : ""));
devDependencies.push("typescript");
}

if (prettier) {
devDependencies.push("prettier");
}
Expand Down Expand Up @@ -372,7 +400,7 @@ async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {
await fs.copy(templateDir, cwd);
});

if (!argv.skipBuild) {
if (template !== InitMode.LuauPackage && !argv.skipBuild) {
await benchmark("Compiling..", () => cmd(selectedPackageManager.build, cwd));
}
}
Expand All @@ -381,6 +409,7 @@ const GAME_DESCRIPTION = "Generate a Roblox place";
const MODEL_DESCRIPTION = "Generate a Roblox model";
const PLUGIN_DESCRIPTION = "Generate a Roblox Studio plugin";
const PACKAGE_DESCRIPTION = "Generate a roblox-ts npm package";
const LUAU_PACKAGE_DESCRIPTION = "Generate an npm package for Luau code";

/**
* Defines behavior of `rbxtsc init` command.
Expand Down Expand Up @@ -439,7 +468,10 @@ export = {
.command([InitMode.Game, InitMode.Place], GAME_DESCRIPTION, {}, argv => init(argv as never, InitMode.Game))
.command(InitMode.Model, MODEL_DESCRIPTION, {}, argv => init(argv as never, InitMode.Model))
.command(InitMode.Plugin, PLUGIN_DESCRIPTION, {}, argv => init(argv as never, InitMode.Plugin))
.command(InitMode.Package, PACKAGE_DESCRIPTION, {}, argv => init(argv as never, InitMode.Package)),
.command(InitMode.Package, PACKAGE_DESCRIPTION, {}, argv => init(argv as never, InitMode.Package))
.command(InitMode.LuauPackage, LUAU_PACKAGE_DESCRIPTION, {}, argv =>
init(argv as never, InitMode.LuauPackage),
),
handler: argv => init(argv, InitMode.None),
// eslint-disable-next-line @typescript-eslint/ban-types
} satisfies yargs.CommandModule<{}, InitOptions>;
7 changes: 7 additions & 0 deletions templates/luau-package/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Module {
sayHello: (name: string) => string;
}

declare const Module: Module;

export = Module;
7 changes: 7 additions & 0 deletions templates/luau-package/src/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local Module = {}

function Module.sayHello(name: string)
return `Hello {name}!`
end

return Module
23 changes: 23 additions & 0 deletions templates/luau-package/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
// required
"allowSyntheticDefaultImports": true,
"jsx": "react",
"jsxFactory": "React.createElement",
"jsxFragmentFactory": "React.Fragment",
"module": "commonjs",
"moduleResolution": "Node",
"noLib": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"moduleDetection": "force",
"strict": true,
"target": "ESNext",
"typeRoots": ["node_modules/@rbxts"],
"declaration": false,

// configurable
"rootDir": "src"
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"target": "ES2019",
"lib": ["ES2019"],
"strict": true,
"noFallthroughCasesInSwitch": true,
"noFallthroughCasesInSwitch": false,
"strictNullChecks": true,
"esModuleInterop": true,
"removeComments": true,
Expand Down