Change build setup of JS clients, reintroduce beta publishing after losing it due to unmerged code (#238)

This commit is contained in:
Janos Dobronszki
2021-10-22 09:49:47 +01:00
committed by GitHub
parent 7cc2a86d0f
commit 293d5ecdde
140 changed files with 13946 additions and 643 deletions

View File

@@ -6,9 +6,36 @@ import (
"strings"
"testing"
"github.com/Masterminds/semver/v3"
"github.com/getkin/kin-openapi/openapi3"
)
func TestSemver(t *testing.T) {
v, _ := semver.NewVersion("0.0.0-beta1")
if incBeta(*v).String() != "0.0.0-beta2" {
t.Fatal(v)
}
v1, _ := semver.NewVersion("0.0.1")
if !v1.GreaterThan(v) {
t.Fatal("no good")
}
v2, _ := semver.NewVersion("0.0.0")
if !v2.GreaterThan(v) {
t.Fatal("no good")
}
if v.String() != "0.0.0-beta1" {
t.Fatal("no good")
}
v3, _ := semver.NewVersion("0.0.0-beta2")
if !v3.GreaterThan(v) {
t.Fatal("no good")
}
}
type tspec struct {
openapi string
tsresult string

View File

@@ -13,6 +13,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
@@ -110,15 +111,14 @@ func main() {
},
}
services := []service{}
tsExportsMap := map[string]string{}
tsFileList := []string{"esm", "index.js", "index.d.ts"}
for _, f := range files {
if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") {
continue
}
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
serviceName := f.Name()
// see https://stackoverflow.com/questions/44345257/import-from-subfolder-of-npm-package
tsExportsMap["./"+serviceName] = "./dist/" + serviceName + "/index.js"
tsFileList = append(tsFileList, serviceName)
serviceDir := filepath.Join(workDir, f.Name())
cmd := exec.Command("make", "api")
cmd.Dir = serviceDir
@@ -187,12 +187,12 @@ func main() {
os.Exit(1)
}
err = os.MkdirAll(filepath.Join(tsPath, serviceName), 0777)
err = os.MkdirAll(filepath.Join(tsPath, "src", serviceName), 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
f, err := os.OpenFile(filepath.Join(tsPath, serviceName, "index.ts"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
f, err := os.OpenFile(filepath.Join(tsPath, "src", serviceName, "index.ts"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
if err != nil {
fmt.Println("Failed to open schema file", err)
os.Exit(1)
@@ -204,7 +204,7 @@ func main() {
os.Exit(1)
}
cmd = exec.Command("prettier", "-w", "index.ts")
cmd.Dir = filepath.Join(tsPath, serviceName)
cmd.Dir = filepath.Join(tsPath, "src", serviceName)
outp, err = cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprintf("Problem formatting '%v' client: %v %s", serviceName, string(outp), err.Error()))
@@ -449,7 +449,7 @@ func main() {
os.Exit(1)
}
tsFiles := filepath.Join(workDir, "cmd", "clients", "ts")
cmd = exec.Command("cp", filepath.Join(tsFiles, "package.json"), filepath.Join(tsFiles, ".gitignore"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "tsconfig.json"), filepath.Join(workDir, "clients", "ts"))
cmd = exec.Command("cp", filepath.Join(tsFiles, "package.json"), filepath.Join(tsFiles, ".gitignore"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "build.js"), filepath.Join(tsFiles, "tsconfig.es.json"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "tsconfig.json"), filepath.Join(workDir, "clients", "ts"))
cmd.Dir = filepath.Join(tsPath)
outp, err = cmd.CombinedOutput()
if err != nil {
@@ -526,6 +526,14 @@ func main() {
type npmVers struct {
Versions []string `json:"versions"`
}
beta := os.Getenv("IS_BETA") != ""
if beta {
fmt.Println("creating beta version")
} else {
fmt.Println("creating live version")
}
npmOutput := &npmVers{}
var latest *semver.Version
if len(outp) > 0 {
@@ -535,6 +543,7 @@ func main() {
os.Exit(1)
}
}
fmt.Println("npm output version: ", npmOutput.Versions)
for _, version := range npmOutput.Versions {
v, err := semver.NewVersion(version)
@@ -548,11 +557,37 @@ func main() {
if v.GreaterThan(latest) {
latest = v
}
}
if latest == nil {
latest, _ = semver.NewVersion("0.0.0")
fmt.Println("found no semver version")
os.Exit(1)
}
var newV semver.Version
if beta {
// bump a beta version
if strings.Contains(latest.String(), "beta") {
newV = incBeta(*latest)
} else {
// make beta out of latest non beta version
v, _ := semver.NewVersion(latest.IncPatch().String() + "-beta1")
newV = *v
}
} else {
newV = latest.IncPatch()
}
// add file list to gitignore
f, err = os.OpenFile(filepath.Join(tsPath, ".gitignore"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0744)
for _, sname := range tsFileList {
_, err := f.Write([]byte(sname + "\n"))
if err != nil {
fmt.Println("failed to append service to gitignore", err)
os.Exit(1)
}
}
newV := latest.IncPatch()
// bump package to latest version
fmt.Println("Bumping to ", newV.String())
@@ -576,7 +611,7 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
m["exports"] = tsExportsMap
m["files"] = tsFileList
pakJS, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println(err)
@@ -594,6 +629,24 @@ func main() {
}
}
func incBeta(ver semver.Version) semver.Version {
s := ver.String()
parts := strings.Split(s, "beta")
if len(parts) < 2 {
panic("not a beta version " + s)
}
i, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
panic(err)
}
i++
v, err := semver.NewVersion(parts[0] + "beta" + fmt.Sprintf("%v", i))
if err != nil {
panic(err)
}
return *v
}
func schemaToType(language, serviceName, typeName string, schemas map[string]*openapi3.SchemaRef) string {
var recurse func(props map[string]*openapi3.SchemaRef, level int) string

View File

@@ -1 +1,9 @@
node_modules
dist
es
lib
types
tmp
index.js
index.d.ts
esm

91
cmd/clients/ts/build.js Normal file
View File

@@ -0,0 +1,91 @@
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const { ncp } = require('ncp');
function getTmpEsmDirectories() {
return fs
.readdirSync('./tmp/esm')
.filter(file => fs.statSync(`./tmp/esm/${file}`).isDirectory());
}
function log(text) {
console.log(`${chalk.cyan('M3O JS:')} ${text}`);
}
function writeModulePackageJsonFile(location) {
fs.writeFileSync(
`${location}/package.json`,
`{"module": "./esm/index.js"}`,
'utf8'
);
}
function deleteDirectory(directory) {
return new Promise(resolve => {
rimraf(directory, err => {
resolve();
});
});
}
function copyAllTmpFolders() {
return new Promise((resolve, reject) => {
// Now copy to root level
ncp(path.join(__dirname, 'tmp'), __dirname, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function moveToLocalEsmFolders() {
return new Promise((resolve, reject) => {
const esmDirs = getTmpEsmDirectories();
// Move the files around in tmp...
esmDirs.forEach(dir => {
const currentPath = path.join(__dirname, 'tmp/esm', dir);
fs.readdirSync(currentPath).forEach(async file => {
const currentFilePath = path.join(currentPath, file);
const newFilePath = path.join(__dirname, 'tmp', dir, 'esm', file);
const esmFolderLocation = path.join(__dirname, 'tmp', dir, 'esm');
try {
if (!fs.existsSync(esmFolderLocation)) {
fs.mkdirSync(esmFolderLocation);
}
fs.renameSync(currentFilePath, newFilePath);
writeModulePackageJsonFile(`./tmp/${dir}`);
await deleteDirectory(`./tmp/esm/${dir}`);
} catch (err) {
reject(err);
}
});
});
log('Moved local esm folders');
resolve();
});
}
async function build() {
log('Moving to correct folders');
try {
await moveToLocalEsmFolders();
await copyAllTmpFolders();
writeModulePackageJsonFile('./tmp/esm');
await deleteDirectory('./tmp');
} catch (e) {
console.log(e);
}
}
build();

File diff suppressed because it is too large Load Diff

View File

@@ -1,26 +1,31 @@
{
"name": "m3o",
"version": "1.0.1",
"description": "",
"main": "dist",
"types": "dist/index.d.ts",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/micro/services"
},
"author": "",
"license": "ISC",
"scripts": {
"build": "tsc",
"prepare": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"typescript": "^3.5.1"
},
"dependencies": {
"@m3o/m3o-node": "^0.0.24"
},
"exports": {}
"name": "m3o",
"version": "1.0.1",
"types": "index.d.ts",
"main": "index.js",
"module": "esm/index.js",
"files": [],
"repository": {
"type": "git",
"url": "https://github.com/micro/services"
},
"author": "",
"license": "ISC",
"scripts": {
"clean": "rimraf ./tmp",
"build": "npm run clean && tsc && tsc --p tsconfig.es.json && node build.js",
"prepare": "npm run build"
},
"dependencies": {
"@m3o/m3o-node": "^0.0.24",
"@types/estree": "^0.0.47",
"chalk": "^2.4.2",
"move-file": "^3.0.0",
"ncp": "^2.0.0",
"rimraf": "^3.0.2"
},
"devDependencies": {
"prettier": "^2.4.1",
"typescript": "^3.5.1"
}
}

View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "ES2015",
"target": "ES6",
"declaration": true,
"lib": ["es2015", "dom"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./tmp/esm",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"include": ["src/index.ts", "src/**/*"],
"exclude": ["./dist", "./node_modules"]
}

View File

@@ -1,16 +1,16 @@
{
"compilerOptions": {
"module": "es6",
"module": "CommonJS",
"target": "es5",
"declaration": true,
"lib": ["es2015", "dom"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"outDir": "./tmp",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"include": ["index.ts", "./*"],
"exclude": ["src/**/*.spec.*", "./dist", "./node_modules"]
"include": ["src/index.ts", "src/**/*"],
"exclude": ["./dist", "./node_modules"]
}

View File

@@ -35,12 +35,12 @@ export interface {{ title $typeName }}{{ "{" }}
{{end}}
`
const tsExampleTemplate = `{{ $service := .service }}import * as {{ $service.ImportName }} from 'm3o/{{ $service.Name }}';
const tsExampleTemplate = `{{ $service := .service }}const { {{ title $service.Name }}Service } = require('m3o/{{ $service.Name }}');
{{ if endpointComment .endpoint $service.Spec.Components.Schemas }}{{ endpointComment .endpoint $service.Spec.Components.Schemas }}{{ end }}async function {{ .funcName }}() {
let {{ $service.Name }}Service = new {{ $service.ImportName }}.{{ title $service.Name }}Service(process.env.MICRO_API_TOKEN)
{{ if endpointComment .endpoint $service.Spec.Components.Schemas }}{{ endpointComment .endpoint $service.Spec.Components.Schemas }}{{ end }}async function {{ untitle .funcName }}() {
let {{ $service.Name }}Service = new {{ title $service.Name }}Service(process.env.MICRO_API_TOKEN)
let rsp = await {{ $service.Name }}Service.{{ .endpoint }}({{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }})
console.log(rsp)
}
await {{ .funcName }}()`
{{ untitle .funcName }}()`