Skip to content

Commit

Permalink
v0.01
Browse files Browse the repository at this point in the history
  • Loading branch information
Xin.Chen committed Mar 31, 2021
0 parents commit 65d1a32
Show file tree
Hide file tree
Showing 20 changed files with 1,070 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.DS_Store
dist
dist-ssr
*.local
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
node_modules
.vscode
.idea
**/node_modules/
**/dist/


16 changes: 16 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**/dist/
**/node_modules/
.DS_Store
dist
dist-ssr
*.local
*.log
*.tgz
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
node_modules
.vscode
.idea

193 changes: 193 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env node

// modified from https://github.com/vitejs/vite/tree/main/packages/create-app

// @ts-check
const fs = require("fs");
const path = require("path");
const argv = require("minimist")(process.argv.slice(2));
const { prompt } = require("enquirer");
const {
yellow,
green,
cyan,
magenta,
lightRed,
red,
stripColors,
} = require("kolorist");

const cwd = process.cwd();

const TEMPLATES = [yellow("cesium"), green("cesium-ts")];

const renameFiles = {
_gitignore: ".gitignore",
};

async function init() {
let targetDir = argv._[0];
if (!targetDir) {
/**
* @type {{ projectName: string }}
*/
const { projectName } = await prompt({
type: "input",
name: "projectName",
message: `Project name:`,
initial: "cesium-project",
});
targetDir = projectName;
}
const packageName = await getValidPackageName(targetDir);
const root = path.join(cwd, targetDir);
console.log(`\nScaffolding project in ${root}...`);

if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true });
} else {
const existing = fs.readdirSync(root);
if (existing.length) {
/**
* @type {{ yes: boolean }}
*/
const { yes } = await prompt({
type: "confirm",
name: "yes",
initial: "Y",
message:
`Target directory ${targetDir} is not empty.\n` +
`Remove existing files and continue?`,
});
if (yes) {
emptyDir(root);
} else {
return;
}
}
}

// determine template
let template = argv.t || argv.template;
let message = "Select a template:";
let isValidTemplate = false;

// --template expects a value
if (typeof template === "string") {
const availableTemplates = TEMPLATES.map(stripColors);
isValidTemplate = availableTemplates.includes(template);
message = `${template} isn't a valid template. Please choose from below:`;
}

if (!template || !isValidTemplate) {
/**
* @type {{ t: string }}
*/
const { t } = await prompt({
type: "select",
name: "t",
message,
choices: TEMPLATES,
});
template = stripColors(t);
}

const templateDir = path.join(__dirname, `template-${template}`);

const write = (file, content) => {
const targetPath = renameFiles[file]
? path.join(root, renameFiles[file])
: path.join(root, file);
if (content) {
fs.writeFileSync(targetPath, content);
} else {
copy(path.join(templateDir, file), targetPath);
}
};

const files = fs.readdirSync(templateDir);
for (const file of files.filter((f) => f !== "package.json")) {
write(file);
}

const pkg = require(path.join(templateDir, `package.json`));

pkg.name = packageName;

write("package.json", JSON.stringify(pkg, null, 2));

const pkgManager = /yarn/.test(process.env.npm_execpath) ? "yarn" : "npm";

console.log(`\nDone. Now run:\n`);
if (root !== cwd) {
console.log(` cd ${path.relative(cwd, root)}`);
}
console.log(` ${pkgManager === "yarn" ? `yarn` : `npm install`}`);
console.log(` ${pkgManager === "yarn" ? `yarn dev` : `npm run dev`}`);
console.log();
}

function copy(src, dest) {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
copyDir(src, dest);
} else {
fs.copyFileSync(src, dest);
}
}

async function getValidPackageName(projectName) {
const packageNameRegExp = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
if (packageNameRegExp.test(projectName)) {
return projectName;
} else {
const suggestedPackageName = projectName
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/^[._]/, "")
.replace(/[^a-z0-9-~]+/g, "-");

/**
* @type {{ inputPackageName: string }}
*/
const { inputPackageName } = await prompt({
type: "input",
name: "inputPackageName",
message: `Package name:`,
initial: suggestedPackageName,
validate: (input) =>
packageNameRegExp.test(input) ? true : "Invalid package.json name",
});
return inputPackageName;
}
}

function copyDir(srcDir, destDir) {
fs.mkdirSync(destDir, { recursive: true });
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file);
const destFile = path.resolve(destDir, file);
copy(srcFile, destFile);
}
}

function emptyDir(dir) {
if (!fs.existsSync(dir)) {
return;
}
for (const file of fs.readdirSync(dir)) {
const abs = path.resolve(dir, file);
// baseline is Node 12 so can't use rmSync :(
if (fs.lstatSync(abs).isDirectory()) {
emptyDir(abs);
fs.rmdirSync(abs);
} else {
fs.unlinkSync(abs);
}
}
}

init().catch((e) => {
console.error(e);
});
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "create-cesium",
"version": "0.0.1",
"license": "MIT",
"author": "nshen <nshen121@gmail.com>",
"bin": {
"create-cesium": "index.js",
"create-cesium-app": "index.js",
"ccesium": "index.js"
},
"main": "index.js",
"engines": {
"node": ">=12.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nshen/create-cesium-app.git"
},
"bugs": {
"url": "https://github.com/nshen/create-cesium-app/issues"
},
"homepage": "https://github.com/nshen/create-cesium-app/tree/main#readme",
"dependencies": {
"enquirer": "^2.3.6",
"kolorist": "^1.2.9",
"minimist": "^1.2.5"
}
}
13 changes: 13 additions & 0 deletions template-cesium-ts/_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
dist
dist-ssr
*.local
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
node_modules
.vscode
.idea

13 changes: 13 additions & 0 deletions template-cesium-ts/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" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>create-cesium</title>
<script type="module" src="/src/index.ts"></script>
</head>
<body>
<div id="cesiumContainer"></div>
</body>
</html>
14 changes: 14 additions & 0 deletions template-cesium-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cesium-starter",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"vite": "^2.1.5",
"vite-plugin-cesium": "^1.2.0",
"cesium": "^1.79.1"
}
}
9 changes: 9 additions & 0 deletions template-cesium-ts/src/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
html,
body,
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
4 changes: 4 additions & 0 deletions template-cesium-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Viewer } from "cesium";
import "./css/main.css";

const viewer = new Viewer("cesiumContainer");
5 changes: 5 additions & 0 deletions template-cesium-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from "vite";
import cesium from "vite-plugin-cesium";
export default defineConfig({
plugins: [cesium()],
});
Loading

0 comments on commit 65d1a32

Please sign in to comment.