Skip to content

Commit

Permalink
Add Luau package template
Browse files Browse the repository at this point in the history
  • Loading branch information
LouieK22 committed Feb 21, 2024
1 parent cbc2d4b commit 24c2b50
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
43 changes: 32 additions & 11 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 @@ -141,10 +142,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(
value => ({
title: value,
value,
}),
),
initial: 0,
},
{
Expand Down Expand Up @@ -225,10 +228,24 @@ 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.LuauPackage) {
pkgJson.scripts = undefined;
} else {
pkgJson.scripts = {
build: "rbxtsc",
watch: "rbxtsc -w",
};
}

if (template === InitMode.LuauPackage) {
pkgJson.name = RBXTS_SCOPE + "/" + pkgJson.name;
pkgJson.main = "lib/init.lua";
pkgJson.types = "lib/index.d.ts";
pkgJson.files = ["lib/init.lua", "lib/index.d.ts"];
pkgJson.publishConfig = { access: "public" };
}

if (template === InitMode.Package) {
pkgJson.name = RBXTS_SCOPE + "/" + pkgJson.name;
pkgJson.main = "out/init.lua";
Expand All @@ -237,6 +254,7 @@ async function init(argv: yargs.Arguments<InitOptions>, initMode: InitMode) {
pkgJson.publishConfig = { access: "public" };
pkgJson.scripts.prepublishOnly = selectedPackageManager.build;
}

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

Expand All @@ -256,12 +274,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 +393,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 Down
7 changes: 7 additions & 0 deletions templates/luau-package/lib/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/lib/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": "lib"
}
}

0 comments on commit 24c2b50

Please sign in to comment.