Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r4ai committed Apr 10, 2024
1 parent 78b8576 commit 630e47f
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 9 deletions.
Binary file modified bun.lockb
Binary file not shown.
8 changes: 6 additions & 2 deletions packages/create-project/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@r4ai/create-project",
"name": "npm-project",
"version": "0.0.0",
"description": "A CLI to create a new project with some templates",
"author": "rai",
Expand All @@ -26,7 +26,7 @@
"LICENSE"
],
"bin": {
"create-project": "./dist/index.js"
"create-project": "dist/index.js"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -56,5 +56,9 @@
"rimraf": "^5.0.5",
"typescript": "^5.0.0",
"vitest": "^1.3.1"
},
"homepage": "https://github.com/r4ai/templates#readme",
"directories": {
"test": "tests"
}
}
11 changes: 6 additions & 5 deletions packages/create-project/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ type Package = {
description?: string;
};

export const templates = () => {
const templatesDir = path.join(
export const templates = (
templatesDirPath = path.join(
import.meta.dirname,
"..",
"..",
"..",
"templates",
);
const templateDirs = getDirectories(templatesDir);
),
) => {
const templateDirs = getDirectories(templatesDirPath);
return templateDirs.map((dir) => {
const packageJsonPath = path.join(templatesDir, dir, "package.json");
const packageJsonPath = path.join(templatesDirPath, dir, "package.json");
const packageJson: Package = JSON.parse(
fs.readFileSync(packageJsonPath, "utf-8"),
);
Expand Down
24 changes: 24 additions & 0 deletions packages/create-project/tests/template.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from "node:path";
import { describe, expect, test } from "vitest";
import { templates } from "../src/template";

const templatesDirPath = path.join(
import.meta.dirname ?? __dirname,
"templates",
);

describe("templates", () => {
test("Get templates", () => {
const result = templates(templatesDirPath);
expect(result).toEqual([
{
name: "npm-project",
value: "npm-project",
},
{
name: "vite-project",
value: "vite-project",
},
]);
});
});
175 changes: 175 additions & 0 deletions packages/create-project/tests/templates/npm-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions packages/create-project/tests/templates/npm-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# npm-project

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.3. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello via Bun!");
11 changes: 11 additions & 0 deletions packages/create-project/tests/templates/npm-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "npm-project",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
27 changes: 27 additions & 0 deletions packages/create-project/tests/templates/npm-project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
24 changes: 24 additions & 0 deletions packages/create-project/tests/templates/vite-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions packages/create-project/tests/templates/vite-project/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions packages/create-project/tests/templates/vite-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "vite-project",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function setupCounter(element: HTMLButtonElement) {
let counter = 0;
const setCounter = (count: number) => {
counter = count;
element.innerHTML = `count is ${counter}`;
};
element.addEventListener("click", () => setCounter(counter + 1));
setCounter(0);
}
Loading

0 comments on commit 630e47f

Please sign in to comment.