@cilix/lightjs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +1010 -0
  2. package/cli.js +163 -0
  3. package/core.js +2730 -0
  4. package/index.js +364 -0
  5. package/package.json +26 -0
package/cli.js ADDED
@@ -0,0 +1,163 @@
1
+ #! /usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const http = require('http');
6
+ const light = require('./index.js');
7
+ const dir = process.cwd();
8
+ const config = {};
9
+
10
+ const help = `
11
+ usage: lightjs [options]
12
+ options:
13
+ create <name> Create a minimal project boilerplate using Lightjs's built-in router
14
+
15
+ dev Start a dev server
16
+ -r <path>=<file> Specify a route to an lightjs file for the dev server
17
+ -p <port> Port for the dev server (defaults to 3838)
18
+ -d <dir> Public directory for the dev server
19
+ `;
20
+
21
+
22
+ const packageFile = (name, version) => `{
23
+ "name": "${name}",
24
+ "version": "0.0.1",
25
+ "description": "",
26
+ "main": "server.js",
27
+ "scripts": {
28
+ "prod": "node server",
29
+ "dev": "node server dev"
30
+ },
31
+ "keywords": [],
32
+ "author": "",
33
+ "license": "ISC",
34
+ "dependencies": {
35
+ "@cilix/lightjs": "^${version}"
36
+ }
37
+ }
38
+ `;
39
+
40
+ const todoFile = `export tag TodoList [
41
+ let item = ''
42
+ let items = props.items
43
+ ---
44
+ const add = () => {
45
+ items.push(item);
46
+ item = '';
47
+ };
48
+ ---
49
+ input bind:value={item} []
50
+ button on:click='add()' 'add'
51
+ ul [
52
+ each (i in items) [
53
+ li '{{i}}'
54
+ ]
55
+ ]
56
+ ]
57
+ `
58
+
59
+ const quickIndex = `import './todo.light'
60
+
61
+ tag Counter [
62
+ let count = 0
63
+ button on:click='count++' 'count: {{count}}'
64
+ ]
65
+
66
+ html [
67
+ head [
68
+ title 'Light.js'
69
+ meta charset='UTF-8' []
70
+ meta name='viewport' content='width=device-width, initial-scale=1.0' []
71
+ meta name='description' content='my site' []
72
+ link rel='stylesheet' href='main.css' []
73
+ ]
74
+ body [
75
+ h1 'Welcome'
76
+ Counter []
77
+ br []
78
+ TodoList items={data.items} []
79
+ ]
80
+ ]
81
+ `;
82
+
83
+ const quickServer = `const http = require('http');
84
+ const light = require('@cilix/lightjs');
85
+
86
+ const prod = !process.argv.includes('dev');
87
+
88
+ const app = light.router({
89
+ publicDir: './public',
90
+ cache: prod,
91
+ minify: prod,
92
+ routes: {
93
+ '/': {
94
+ input: 'index.light',
95
+ data: {
96
+ items: [
97
+ 'wake up',
98
+ 'walk dog'
99
+ ]
100
+ }
101
+ }
102
+ }
103
+ });
104
+
105
+ http.createServer(app).listen(4477, () => {
106
+ console.log('Listening on port 4477...');
107
+ });
108
+ `;
109
+
110
+ const cssFile = `body {
111
+ font-family: "Helvetica Neue", Arial, sans-serif;
112
+ margin: 20px;
113
+ }
114
+ `
115
+
116
+ for (let i = 0; i < process.argv.length; i++) {
117
+ switch (process.argv[i]) {
118
+ case 'create':
119
+ config.create = process.argv[++i];
120
+ break
121
+ case 'dev':
122
+ config.dev = true;
123
+ break;
124
+ case '-d':
125
+ config.publicDir = process.argv[++i];
126
+ break;
127
+ case '-p':
128
+ config.port= process.argv[++i];
129
+ break;
130
+ case '-r':
131
+ const r = process.argv[++i];
132
+ if (r) {
133
+ const parts = r.split('=');
134
+ if (parts.length === 2) {
135
+ if (!config.routes) config.routes = {};
136
+ config.routes[parts[0]] = { input: parts[1] };
137
+ }
138
+ }
139
+ break;
140
+ default:
141
+ break
142
+ }
143
+ }
144
+ if (config.dev) {
145
+ http.createServer(light.router({
146
+ publicDir: config.publicDir || '.',
147
+ routes: config.routes
148
+ })).listen(4477, () => {
149
+ console.log('Listening on port 4477');
150
+ });
151
+ } else if (config.create) {
152
+ const p = path.resolve(dir, config.create);
153
+ const pf = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8'));
154
+ fs.mkdirSync(p);
155
+ fs.mkdirSync(path.join(p, 'public'));
156
+ fs.writeFileSync(path.join(p, 'public/main.css'), cssFile);
157
+ fs.writeFileSync(path.join(p, 'index.light'), quickIndex);
158
+ fs.writeFileSync(path.join(p, 'todo.light'), todoFile);
159
+ fs.writeFileSync(path.join(p, 'server.js'), quickServer);
160
+ fs.writeFileSync(path.join(p, 'package.json'), packageFile(config.create, pf.version));
161
+ } else {
162
+ console.log(help);
163
+ }