@syndicate-lang/create package

This commit is contained in:
Tony Garnock-Jones 2022-04-22 13:54:49 +02:00
parent c66168206b
commit 9c4e96d585
27 changed files with 295 additions and 139 deletions

10
packages/create/README.md Normal file
View File

@ -0,0 +1,10 @@
# @syndicate-lang/create
<!-- Use `npx @syndicate-lang/create DIRECTORY` or `npm init @syndicate-lang DIRECTORY`: -->
To create a new program/library using Syndicate/js, use `yarn create @syndicate-lang DIRECTORY`:
yarn create @syndicate-lang myprogram
cd myprogram
yarn
node lib/index.js

10
packages/create/bin/run Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env node
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2018-2022 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
try {
require('../lib/create.js').main(process.argv.slice(2));
} catch (e) {
console.error(e);
process.exit(1);
}

View File

@ -0,0 +1,35 @@
{
"name": "@syndicate-lang/create",
"version": "0.11.0",
"description": "Create a new Syndicate/js package",
"author": "Tony Garnock-Jones <tonyg@leastfixedpoint.com>",
"homepage": "https://github.com/syndicate-lang/syndicate-js/tree/main/packages/create",
"license": "GPL-3.0+",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://git.syndicate-lang.org/syndicate-lang/syndicate-js"
},
"bin": {
"create": "./bin/run"
},
"scripts": {
"prepare": "yarn compile && yarn prepare-template",
"compile": "../../node_modules/.bin/tsc",
"compile:watch": "../../node_modules/.bin/tsc -w",
"prepare-template": "mkdir -p lib && cd template && zip -q -r ../lib/syndicate-template.zip .",
"clean": "rm -rf lib",
"veryclean": "yarn run clean && rm -rf node_modules"
},
"main": "lib/create.js",
"dependencies": {
"unzipper": "^0.10.11",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/unzipper": "^0.10.5",
"@types/yargs": "^15.0"
}
}

View File

@ -0,0 +1,43 @@
import yargs from 'yargs/yargs';
import fs from 'fs';
import path from 'path';
import unzip from 'unzipper';
export type CommandLineArguments = {
directory: string,
package?: string,
};
export async function main(argv: string[]) {
const options: CommandLineArguments = yargs(argv)
.command('$0 <directory>',
'Create a new package configured for TypeScript and Syndicate/js',
yargs => yargs
.positional('directory', {
type: 'string',
description: 'Directory in which to place the new package',
}).demandOption('directory')
.option('package', {
alias: 'p',
type: 'string',
description: 'Package name to create; defaults to directory given',
}),
argv => argv)
.argv;
const directory = path.resolve(options.directory);
const packageName = options.package ?? path.basename(directory);
console.log(`Creating package ${packageName} in ${directory}.`);
fs.mkdirSync(directory, { recursive: true });
fs.createReadStream(path.join(__dirname, 'syndicate-template.zip')).pipe(
unzip.Extract({ path: directory }).on('close', (err: any) => {
if (err) throw err;
const packageJsonPath = path.join(directory, 'package.json');
const p = Object.assign({
name: packageName,
}, require(packageJsonPath));
fs.writeFileSync(packageJsonPath, JSON.stringify(p, null, 2));
}));
}

View File

@ -0,0 +1,22 @@
((typescript-mode
. ((eval
. (progn
;; For TIDE:
(setq tide-tsserver-executable
(concat
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d)))
"node_modules/typescript/lib/tsserver.js"))
;; For LSP:
(require 'lsp-javascript)
(let ((node-modules (concat
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d)))
"node_modules/")))
(lsp-dependency 'typescript-language-server
`(:system ,(concat node-modules
"typescript-language-server/lib/cli.js")))
(lsp-dependency 'typescript
`(:system ,(concat node-modules
"typescript/lib/tsserver.js")))))
))))

6
packages/create/template/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
index.js
index.js.map
lib/
node_modules/
src/gen/
src.ts/

View File

@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="style.css" rel="stylesheet">
<script src="node_modules/@preserves/core/dist/preserves.js"></script>
<script src="node_modules/@syndicate-lang/core/dist/syndicate.js"></script>
<script src="node_modules/@syndicate-lang/html/dist/syndicate-html.js"></script>
<script src="node_modules/@syndicate-lang/ws-relay/dist/syndicate-ws-relay.js"></script>
<script src="index.js"></script>
</head>
<body>
<main id='main'></main>
<script>Main.main();</script>
</body>
</html>

View File

@ -0,0 +1,30 @@
{
"private": true,
"version": "0.0.0",
"license": "GPL-3.0+",
"devDependencies": {
"@preserves/core": ">=0.20",
"@preserves/schema": ">=0.21",
"@syndicate-lang/ts-plugin": "*",
"@syndicate-lang/tsc": "*",
"rollup": "^2.60",
"rollup-plugin-sourcemaps": "^0.6",
"typescript": "^4.5"
},
"dependencies": {
"@syndicate-lang/core": "*",
"@syndicate-lang/html": "*",
"@syndicate-lang/ws-relay": "*"
},
"scripts": {
"prepare": "yarn regenerate && yarn compile && yarn rollup",
"regenerate": "rm -rf ./src/gen && preserves-schema-ts --output ./src/gen ./protocols/schemas",
"regenerate:watch": "yarn regenerate --watch",
"compile": "syndicate-tsc",
"compile:watch": "syndicate-tsc -w --verbose --intermediate-directory src.ts",
"rollup": "rollup -c",
"rollup:watch": "rollup -c -w",
"clean": "rm -rf lib/ src.ts/ src/gen/ index.js index.js.map",
"serve": "python -m http.server"
}
}

View File

@ -0,0 +1,3 @@
version 1 .
ExampleDefinition = <example @v string> .

View File

@ -0,0 +1,18 @@
import sourcemaps from 'rollup-plugin-sourcemaps';
export default {
input: 'lib/index.js',
plugins: [sourcemaps()],
output: {
file: 'index.js',
format: 'umd',
name: 'Main',
sourcemap: true,
globals: {
'@preserves/core': 'Preserves',
'@syndicate-lang/core': 'Syndicate',
'@syndicate-lang/html': 'SyndicateHtml',
'@syndicate-lang/ws-relay': 'SyndicateWsRelay',
},
},
};

View File

@ -0,0 +1,52 @@
import { Dataspace, Ref, Sturdy, Reader, Schemas, Embedded } from "@syndicate-lang/core";
import html from "@syndicate-lang/html";
import wsRelay from "@syndicate-lang/ws-relay";
import { ExampleDefinition } from './gen/example';
export function main() {
Dataspace.boot(ds => {
html.boot(ds);
wsRelay.boot(ds, true /* remove this `true` to turn off client/server debug logging */);
bootApp(ds);
});
}
function bootApp(ds: Ref) {
spawn named 'app' {
at ds {
/*
* This example expects a syndicate-server instance running on port 9001 on the
* server hosting index.html, exposing a dataspace entity via a capability called
* `"syndicate"` with empty "secret". See syndicate-server.config.pr.
*/
const serverCap = Sturdy.asSturdyRef(new Reader<Ref>(
'<ref "syndicate" [] #[pkgN9TBmEd3Q04grVG4Zdw==]>').next());
const this_instance = crypto.randomUUID();
during wsRelay.Resolved({
"addr": wsRelay.RelayAddress(Schemas.transportAddress.WebSocket(
`ws://${document.location.hostname}:9001/`)),
"sturdyref": serverCap,
"resolved": $remoteDs_e: Embedded,
}) => {
const remoteDs = remoteDs_e.embeddedValue;
at remoteDs {
assert ExampleDefinition(this_instance);
during ExampleDefinition($who: string) => {
console.log('saw', who);
const ui = new html.Anchor();
at ds {
assert ui.html(
'#main',
html.template`<p>We see <span class="example-definition">${who}</span></p>`,
who);
}
}
}
}
}
}
}

View File

View File

@ -0,0 +1,12 @@
; Start the server with
;
; syndicate-server -c ./syndicate-server.config.pr
;---------------------------------------------------------------------------
; Expose the gatekeeper on port 9001:
<require-service <relay-listener <tcp "0.0.0.0" 9001> $gatekeeper>>
; Create a dataspace entity, and register it with the gatekeeper with name `"syndicate"` and an
; empty secret key:
let ?ds = dataspace
<bind "syndicate" #x"" $ds>

View File

@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["ES2017", "DOM"],
"declaration": true,
"baseUrl": "./src",
"rootDir": "./src",
"outDir": "./lib",
"declarationDir": "./lib",
"esModuleInterop": true,
"moduleResolution": "node",
"module": "es6",
"sourceMap": true,
"strict": true,
"plugins": [
{ "name": "@syndicate-lang/ts-plugin" }
]
},
"include": ["src/**/*"]
}

View File

@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["ES2019", "DOM"],
"declaration": true,
"baseUrl": "./src",
"rootDir": "./src",
"outDir": "./lib",
"declarationDir": "./lib",
"esModuleInterop": true,
"moduleResolution": "node",
"module": "CommonJS",
"sourceMap": true,
"strict": true
},
"include": ["src/**/*"]
}

View File

@ -1,8 +0,0 @@
*-debug.log
*-error.log
/.nyc_output
/dist
/package-lock.json
/tmp
node_modules
/src/syndicate-template.zip

View File

@ -1 +0,0 @@
redo-ifchange src/syndicate-template.zip

View File

@ -1,4 +0,0 @@
#!/usr/bin/env node
require('..').run()
.catch(require('@oclif/errors/handle'))

View File

@ -1,3 +0,0 @@
@echo off
node "%~dp0\run" %*

View File

@ -1 +0,0 @@
rm -f src/syndicate-template.zip

View File

@ -1,32 +0,0 @@
{
"name": "@syndicate-lang/create",
"description": "Create a new Syndicate/js package",
"version": "0.0.5",
"author": "Tony Garnock-Jones <tonyg@leastfixedpoint.com>",
"bin": {
"create": "./bin/run"
},
"scripts": {
"prepare": "which redo >/dev/null && redo || ../../do"
},
"dependencies": {
"@oclif/command": "^1",
"@oclif/config": "^1",
"@oclif/plugin-help": "^2",
"unzipper": "^0.10.11"
},
"homepage": "https://github.com/syndicate-lang/syndicate-js/tree/main/packages/create",
"license": "GPL-3.0+",
"publishConfig": {
"access": "public"
},
"main": "src/index.js",
"oclif": {
"bin": "create"
},
"repository": "github:syndicate-lang/syndicate-js",
"files": [
"/bin",
"/src"
]
}

View File

@ -1,44 +0,0 @@
const {Command, flags} = require('@oclif/command')
const path = require('path');
const fs = require('fs');
const unzip = require('unzipper');
class SyndicateLangCreateCommand extends Command {
async run() {
const { args, flags} = this.parse(SyndicateLangCreateCommand)
const directory = path.resolve(args.directory);
const packageName = flags.package || path.basename(directory);
console.log(`Creating package ${packageName} in ${directory}.`);
fs.mkdir(directory, { recursive: true }, (err) => {
if (err) throw err;
fs.createReadStream(path.join(__dirname, 'syndicate-template.zip')).pipe(
unzip.Extract({ path: directory }).on('close', (err) => {
if (err) throw err;
const packageJsonPath = path.join(directory, 'package.json');
const p = Object.assign({
name: packageName,
}, require(packageJsonPath));
fs.writeFile(packageJsonPath, JSON.stringify(p, null, 2), (err) => {
if (err) throw err;
});
}));
});
}
}
SyndicateLangCreateCommand.id = ''; // ???
SyndicateLangCreateCommand.args = [
{ name: 'directory', required: true },
];
SyndicateLangCreateCommand.description = `Create a new Syndicate/js package.`
SyndicateLangCreateCommand.flags = {
// add --version flag to show CLI version
version: flags.version({char: 'v'}),
// add --help flag to show CLI version
help: flags.help({char: 'h'}),
package: flags.string({char: 'n', description: 'package name to create'}),
}
module.exports = SyndicateLangCreateCommand

View File

@ -1,3 +0,0 @@
redo-ifchange $(find ../template)
target="$(pwd)/$3"
(cd ../template; zip -q -r "$target" .) >&2

View File

@ -1,8 +0,0 @@
{
"presets": [ "@babel/preset-env" ],
"plugins": [
"@syndicate-lang/syntax/plugin",
"@babel/plugin-transform-react-jsx",
"@babel/plugin-syntax-jsx"
]
}

View File

@ -1,18 +0,0 @@
{
"private": true,
"version": "0.0.0",
"license": "GPL-3.0+",
"devDependencies": {
"@babel/core": "^7",
"@babel/plugin-syntax-jsx": "^7",
"@babel/plugin-transform-react-jsx": "^7",
"@babel/preset-env": "^7",
"@syndicate-lang/syntax": ""
},
"dependencies": {
"@syndicate-lang/core": ""
},
"scripts": {
"build": "./node_modules/.bin/syndicate-babel src --out-dir lib"
}
}

View File

@ -1,17 +0,0 @@
"use strict";
import { Observe } from "@syndicate-lang/core";
message type Greeting(who);
spawn named 'sender' {
on asserted Observe(Greeting(_)) {
send Greeting("world");
}
}
spawn named 'greeter' {
on message Greeting($who) {
console.log(`Hello, ${who}!`);
}
}