From db53d12c8e8132cf45549d26b7aecf02aabc6458 Mon Sep 17 00:00:00 2001 From: Gefei Hou Date: Tue, 27 Aug 2024 10:35:54 +0800 Subject: [PATCH] Windows support (except for starship folder) --- packages/ast/test-utils/index.ts | 2 +- packages/parser/src/store.ts | 10 ++++++---- packages/parser/src/utils.ts | 7 ++++++- packages/telescope/__tests__/telescope.test.ts | 2 +- packages/telescope/src/builder.ts | 18 ++++++++++++++---- packages/telescope/src/bundle.ts | 4 ++-- packages/telescope/src/commands/generate.ts | 2 +- packages/telescope/src/commands/install.ts | 2 +- packages/telescope/src/contracts/install.ts | 2 +- packages/telescope/src/protod/bufbuild.ts | 6 +++--- packages/telescope/src/protod/recursive.ts | 4 ++-- packages/telescope/src/protod/utils.ts | 4 ++-- packages/telescope/src/utils/index.ts | 5 +++++ packages/telescope/test-utils/index.ts | 4 ++-- 14 files changed, 47 insertions(+), 25 deletions(-) diff --git a/packages/ast/test-utils/index.ts b/packages/ast/test-utils/index.ts index dc672b50d0..a5dae2e53c 100644 --- a/packages/ast/test-utils/index.ts +++ b/packages/ast/test-utils/index.ts @@ -45,7 +45,7 @@ export const getTestProtoStore = (options?: TelescopeOptions) => { } export const getMiscTestProtoStore = (options?: TelescopeOptions) => { - const store = new ProtoStore([__dirname + '/../../../__fixtures__/misc/proto'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); + const store = new ProtoStore([__dirname.replace(/\\/g,'/') + '/../../../__fixtures__/misc/proto'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); return store; } diff --git a/packages/parser/src/store.ts b/packages/parser/src/store.ts index ff8e3388c3..c7eeee13cd 100644 --- a/packages/parser/src/store.ts +++ b/packages/parser/src/store.ts @@ -1,4 +1,4 @@ -import { sync as glob } from 'glob'; +import { glob } from './utils'; import { parse } from '@cosmology/protobufjs'; import { readFileSync } from 'fs'; import { join, resolve as pathResolve } from 'path'; @@ -69,7 +69,9 @@ export class ProtoStore implements IProtoStore { }> = {}; constructor(protoDirs: string[] = [], options: TelescopeOptions = defaultTelescopeOptions) { - this.protoDirs = protoDirs.map(protoDir => pathResolve(protoDir)); + this.protoDirs = protoDirs.map((protoDir) => { + return pathResolve(protoDir).replace(/\\/g,'/') + }); this.options = options; } @@ -135,11 +137,11 @@ export class ProtoStore implements IProtoStore { getProtos(): ProtoRef[] { if (this.protos) return this.protos; const contents = this.protoDirs.reduce((m, protoDir) => { - const protoSplat = join(protoDir, '/**/*.proto'); + const protoSplat = join(protoDir, '/**/*.proto').replace(/\\/g,'/'); const protoFiles = glob(protoSplat); const contents = protoFiles.map(filename => ({ absolute: filename, - filename: filename.split(protoDir)[1].replace(/^\//, ''), + filename: filename.split(protoDir.replace(/\\/g,'/'))[1].replace(/^\//, ''), content: readFileSync(filename, 'utf-8') })); return [...m, ...contents]; diff --git a/packages/parser/src/utils.ts b/packages/parser/src/utils.ts index c393609163..4c62686355 100644 --- a/packages/parser/src/utils.ts +++ b/packages/parser/src/utils.ts @@ -4,6 +4,7 @@ import { ProtoStore } from './store'; import { getTypeUrl, getAminoTypeNameByRef, getTypeNameFromFieldName } from '@cosmology/utils'; import { getNestedProto } from '.'; export { isRefExcluded, isRefIncluded, getObjectName } from '@cosmology/utils' +import { sync as globSync } from 'glob'; // https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.cc#L3798-L3812 // NOTE: sometimes you need to pass in `.Dummy` for the first call, @@ -221,4 +222,8 @@ export const createEmptyProtoRef = (pkg?: string, filename?: string): ProtoRef = symbols: null } } -}; \ No newline at end of file +}; + +export const glob = (input:string, options?:object) => { + return globSync(input.replace(/\\/g,'/'), options); + } \ No newline at end of file diff --git a/packages/telescope/__tests__/telescope.test.ts b/packages/telescope/__tests__/telescope.test.ts index 70f218e679..a8d09c5f0f 100644 --- a/packages/telescope/__tests__/telescope.test.ts +++ b/packages/telescope/__tests__/telescope.test.ts @@ -282,7 +282,7 @@ const options: TelescopeOptions = { const input: TelescopeInput = { outPath, - protoDirs: [__dirname + '/../../../__fixtures__/chain1'], + protoDirs: [__dirname.replace(/\\/g,'/') + '/../../../__fixtures__/chain1'], options }; diff --git a/packages/telescope/src/builder.ts b/packages/telescope/src/builder.ts index 5a6c90566e..9e98a71564 100644 --- a/packages/telescope/src/builder.ts +++ b/packages/telescope/src/builder.ts @@ -31,7 +31,14 @@ import { plugin as createRpcOpsBundle } from './generators/create-rpc-ops-bundle const sanitizeOptions = (options: TelescopeOptions): TelescopeOptions => { // If an element at the same key is present for both x and y, the value from y will appear in the result. options = deepmerge(defaultTelescopeOptions, options ?? {}); - + // correct the path for windows + if(options.cosmwasm){ + options.cosmwasm.outPath = options.cosmwasm.outPath.replace(/\\/g,'/') + options.cosmwasm.contracts = options.cosmwasm.contracts.map((item:{name:string, dir:string})=>{ + item.dir = item.dir.replace(/\\/g,'/') + return item + }) + } // strip off leading slashes options.tsDisable.files = options.tsDisable.files.map((file) => file.startsWith('/') ? file : file.replace(/^\//, '') @@ -67,10 +74,13 @@ export class TelescopeBuilder { store, options }: TelescopeInput & { store?: ProtoStore }) { - this.protoDirs = protoDirs; - this.outPath = resolve(outPath); + const fixedDirs = protoDirs.map((directory)=>{ + return directory.replace(/\\/g,'/') + }); + this.protoDirs = fixedDirs + this.outPath = resolve(outPath.replace(/\\/g,'/')); this.options = sanitizeOptions(options); - this.store = store ?? new ProtoStore(protoDirs, this.options); + this.store = store ?? new ProtoStore(fixedDirs, this.options); this.store.traverseAll(); } diff --git a/packages/telescope/src/bundle.ts b/packages/telescope/src/bundle.ts index f0457c523d..1b7b020a97 100644 --- a/packages/telescope/src/bundle.ts +++ b/packages/telescope/src/bundle.ts @@ -39,7 +39,7 @@ export const bundlePackages = (store: ProtoStore) => { return Object.keys(allPackages).map(base => { const pkgs = allPackages[base]; const bundleVariables = {}; - const bundleFile = join(base, 'bundle.ts'); + const bundleFile = join(base, 'bundle.ts').replace(/\\/g,'/'); const importPaths = []; parsePackage(store.options, pkgs, bundleFile, importPaths, bundleVariables); return { @@ -153,7 +153,7 @@ export const createFileBundle = ( let rel = relative(dirname(bundleFile), filename); if (!rel.startsWith('.')) rel = `./${rel}`; const variable = `_${counter++}`; - importPaths.push(importNamespace(variable, rel)); + importPaths.push(importNamespace(variable, rel.replace(/\\/g,'/'))); dotty.put(bundleVariables, pkg + '.__export', true); dotty.put(bundleVariables, pkg + '.' + variable, true); } \ No newline at end of file diff --git a/packages/telescope/src/commands/generate.ts b/packages/telescope/src/commands/generate.ts index 60686ec652..8240920b98 100644 --- a/packages/telescope/src/commands/generate.ts +++ b/packages/telescope/src/commands/generate.ts @@ -1,8 +1,8 @@ import * as shell from 'shelljs'; import { prompt } from '../prompt'; import dargs from 'dargs'; +import { glob } from '../utils/index'; -const glob = require('glob').sync; const fs = require('fs'); const path = require('path'); const repo = 'https://github.com/cosmology-tech/telescope-module-boilerplate.git'; diff --git a/packages/telescope/src/commands/install.ts b/packages/telescope/src/commands/install.ts index 2ccb640a61..c68975931c 100644 --- a/packages/telescope/src/commands/install.ts +++ b/packages/telescope/src/commands/install.ts @@ -1,6 +1,6 @@ import { resolve, join, dirname, basename, extname } from 'path'; import { mkdirp } from 'mkdirp'; -import { sync as glob } from 'glob'; +import { glob } from '../utils/index'; import { rimrafSync as rimraf } from 'rimraf'; import { exec } from 'shelljs'; import { prompt } from '../prompt'; diff --git a/packages/telescope/src/contracts/install.ts b/packages/telescope/src/contracts/install.ts index 0b13a2a5a6..773f71ea87 100644 --- a/packages/telescope/src/contracts/install.ts +++ b/packages/telescope/src/contracts/install.ts @@ -1,5 +1,5 @@ import { resolve, join, dirname, basename, extname } from 'path'; -import { sync as glob } from 'glob'; +import { glob } from '../utils/index'; import { mkdirp } from 'mkdirp'; import { rimrafSync as rimraf } from 'rimraf'; import { exec } from 'shelljs'; diff --git a/packages/telescope/src/protod/bufbuild.ts b/packages/telescope/src/protod/bufbuild.ts index 95868a3e24..c9f223249e 100644 --- a/packages/telescope/src/protod/bufbuild.ts +++ b/packages/telescope/src/protod/bufbuild.ts @@ -2,7 +2,7 @@ import fs from "fs"; import { Repo } from "./types"; import YAML from "yaml"; import { isPathExist } from "./utils"; -import { sync as globSync } from "glob"; +import { glob } from '../utils/index'; export function parseBufLockFile(filePath: string): Repo[] { if (!isPathExist(filePath)) { @@ -47,13 +47,13 @@ export function parseBufYamlFile(filePath: string): Repo[] { } export function findAllBufYamlFiles(dir: string): string[] { - return globSync(`${dir}/**/buf.yaml`, { + return glob(`${dir}/**/buf.yaml`, { ignore: `${dir}/node_modules/**`, }); } export function findAllBufLockFiles(dir: string): string[] { - return globSync(`${dir}/**/buf.lock`, { + return glob(`${dir}/**/buf.lock`, { ignore: `${dir}/node_modules/**`, }); } diff --git a/packages/telescope/src/protod/recursive.ts b/packages/telescope/src/protod/recursive.ts index 5f83785cff..a57f85fa07 100644 --- a/packages/telescope/src/protod/recursive.ts +++ b/packages/telescope/src/protod/recursive.ts @@ -9,7 +9,7 @@ import { import { join, dirname, resolve } from "path"; import { getCorrespondingGit, makeDir, parseProtoFile } from "./utils"; import fs from "fs"; -import { sync as globSync } from "glob"; +import { glob } from '../utils/index'; export async function cloneAll({ repos, @@ -131,7 +131,7 @@ function extractProtoFromDirs({ for (const target of targets) { for (const source of Object.values(sources)) { - const files = globSync(join(source.protoPath, target)); + const files = glob(join(source.protoPath, target)); extractProtoFiles.push( ...files diff --git a/packages/telescope/src/protod/utils.ts b/packages/telescope/src/protod/utils.ts index 75ecce9173..72d28d475f 100644 --- a/packages/telescope/src/protod/utils.ts +++ b/packages/telescope/src/protod/utils.ts @@ -2,7 +2,7 @@ import { exec as _exec } from "shelljs"; import fs from "fs"; import { bufInfo } from "./config"; import { Repo } from "./types"; -import { sync as globSync } from "glob"; +import { glob } from '../utils/index'; export function exec(command: string, verbose = false) { const { code, stdout, stderr } = _exec(command); @@ -76,7 +76,7 @@ export function isPathExist(path: string): boolean { } export function findAllProtoFiles(dir: string): string[] { - return globSync(`${dir}/**/*.proto`, { + return glob(`${dir}/**/*.proto`, { ignore: `${dir}/node_modules/**`, }); } diff --git a/packages/telescope/src/utils/index.ts b/packages/telescope/src/utils/index.ts index 1ed202fc6d..617d2c580e 100644 --- a/packages/telescope/src/utils/index.ts +++ b/packages/telescope/src/utils/index.ts @@ -2,6 +2,7 @@ import { ProtoRoot, ProtoRef } from '@cosmology/types'; import { relative, dirname, extname } from 'path'; import { ImportObj } from '../types'; import { restoreExtension } from '@cosmology/utils'; +import { sync as globSync } from 'glob'; export const getRoot = (ref: ProtoRef): ProtoRoot => { if (ref.traversed) return ref.traversed; @@ -123,4 +124,8 @@ export const getRelativePath = (f1: string, f2: string, ext?: string) => { return restoreExtension(importPath, ext); } +export const glob = (input:string, options?:object) => { + return globSync(input.replace(/\\/g,'/'), options); +} + export * from './common-create-bundle'; diff --git a/packages/telescope/test-utils/index.ts b/packages/telescope/test-utils/index.ts index 01b58670f6..26335a1e67 100644 --- a/packages/telescope/test-utils/index.ts +++ b/packages/telescope/test-utils/index.ts @@ -38,12 +38,12 @@ const defaultTelescopeOptionsForTesting = { const defaultTelescopeOptions = deepmerge(teleDefaults, defaultTelescopeOptionsForTesting); export const getTestProtoStore = (options?: TelescopeOptions) => { - const store = new ProtoStore([__dirname + '/../../../__fixtures__/chain1'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); + const store = new ProtoStore([__dirname.replace(/\\/g,'/') + '/../../../__fixtures__/chain1'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); return store; } export const getTestProtoStore2 = (options?: TelescopeOptions) => { - const store = new ProtoStore([__dirname + '/../../../__fixtures__/chain2'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); + const store = new ProtoStore([__dirname.replace(/\\/g,'/') + '/../../../__fixtures__/chain2'], options ? deepmerge(defaultTelescopeOptions, options) : deepmerge(defaultTelescopeOptions, {})); return store; }