Skip to content

Commit

Permalink
fix:windows support(exluded starship folder)
Browse files Browse the repository at this point in the history
  • Loading branch information
NorOldBurden committed Sep 11, 2024
1 parent 9e053b7 commit bd06b14
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/ast/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions packages/parser/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sync as glob } from 'glob';
import { crossGlob as glob } from './utils';
import { parse } from '@cosmology/protobufjs';
import { readFileSync } from 'fs';
import { join, resolve as pathResolve } from 'path';
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand Down
15 changes: 14 additions & 1 deletion packages/parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ 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';

const posixPath = require('path').posix;

// 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,
Expand Down Expand Up @@ -221,4 +224,14 @@ export const createEmptyProtoRef = (pkg?: string, filename?: string): ProtoRef =
symbols: null
}
}
};
};

// Replace all \\ to / for windows support purpose
export const crossGlob = (input:string, options?:object) => {
return globSync(input.replace(/\\/g,'/'), options);
}

// Unify all the path to posixPath for windows support purpose
export const toPosixPath = (mixedPath): string => {
return mixedPath.replace(/\\/g, posixPath.sep);
}
2 changes: 2 additions & 0 deletions packages/parser/types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export declare const instanceType: (obj: any) => {
* @returns
*/
export declare const createEmptyProtoRef: (pkg?: string, filename?: string) => ProtoRef;
export declare const crossGlob: (input: string, options?: object) => string[];
export declare const toPosixPath: (mixedPath: any) => string;
2 changes: 1 addition & 1 deletion packages/telescope/__tests__/telescope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ const options: TelescopeOptions = {

const input: TelescopeInput = {
outPath,
protoDirs: [__dirname + '/../../../__fixtures__/chain1'],
protoDirs: [__dirname.replace(/\\/g,'/') + '/../../../__fixtures__/chain1'],
options
};

Expand Down
19 changes: 15 additions & 4 deletions packages/telescope/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ import { plugin as createCosmWasmBundle } from './generators/create-cosmwasm-bun
import { plugin as createPiniaStore } from './generators/create-pinia-store'
import { plugin as createPiniaStoreBundle } from './generators/create-pinia-store-bundle'
import { plugin as createRpcOpsBundle } from './generators/create-rpc-ops-bundle'
import { toPosixPath } from './utils';

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 = toPosixPath(options.cosmwasm.outPath)
options.cosmwasm.contracts = options.cosmwasm.contracts.map((item:{name:string, dir:string})=>{
item.dir = toPosixPath(item.dir)
return item
})
}
// strip off leading slashes
options.tsDisable.files = options.tsDisable.files.map((file) =>
file.startsWith('/') ? file : file.replace(/^\//, '')
Expand Down Expand Up @@ -67,10 +75,13 @@ export class TelescopeBuilder {
store,
options
}: TelescopeInput & { store?: ProtoStore }) {
this.protoDirs = protoDirs;
this.outPath = resolve(outPath);
const fixedDirs = protoDirs.map((directory)=>{
return toPosixPath(directory)
});
this.protoDirs = fixedDirs
this.outPath = resolve(toPosixPath(outPath));
this.options = sanitizeOptions(options);
this.store = store ?? new ProtoStore(protoDirs, this.options);
this.store = store ?? new ProtoStore(fixedDirs, this.options);
this.store.traverseAll();
}

Expand Down
5 changes: 3 additions & 2 deletions packages/telescope/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TelescopeBuilder } from './index';
import { join, relative, dirname } from 'path';
import { TelescopeParseContext } from './build';
import { TelescopeOptions } from '@cosmology/types';
import { toPosixPath } from './utils';

// TODO move to store
export const getPackages = (store: ProtoStore) => {
Expand Down Expand Up @@ -39,7 +40,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 = toPosixPath(join(base, 'bundle.ts'));
const importPaths = [];
parsePackage(store.options, pkgs, bundleFile, importPaths, bundleVariables);
return {
Expand Down Expand Up @@ -153,7 +154,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, toPosixPath(rel)));
dotty.put(bundleVariables, pkg + '.__export', true);
dotty.put(bundleVariables, pkg + '.' + variable, true);
}
2 changes: 1 addition & 1 deletion packages/telescope/src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as shell from 'shelljs';
import { prompt } from '../prompt';
import dargs from 'dargs';
import { crossGlob as 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';
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve, join, dirname, basename, extname } from 'path';
import { mkdirp } from 'mkdirp';
import { sync as glob } from 'glob';
import { crossGlob as glob } from '../utils/index';
import { rimrafSync as rimraf } from 'rimraf';
import { exec } from 'shelljs';
import { prompt } from '../prompt';
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope/src/contracts/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve, join, dirname, basename, extname } from 'path';
import { sync as glob } from 'glob';
import { crossGlob as glob } from '../utils/index';
import { mkdirp } from 'mkdirp';
import { rimrafSync as rimraf } from 'rimraf';
import { exec } from 'shelljs';
Expand Down
10 changes: 9 additions & 1 deletion packages/telescope/src/generators/create-stargate-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ProtoRef } from '@cosmology/types';
import { camel, pascal } from 'case';
import { duplicateImportPathsWithExt, variableSlug } from '@cosmology/utils';
import { buildAllImportsFromGenericContext } from '../imports';
import { toPosixPath } from '../utils';

export const plugin = (
builder: TelescopeBuilder,
Expand Down Expand Up @@ -103,11 +104,18 @@ export const plugin = (
.concat(clientOptions)
.concat(clientBody);

// replace all backslash path for windows
for (let i = 0; i < cProg.length; i++) {
if(cProg[i].source?.value){
cProg[i].source.value = toPosixPath(cProg[i].source?.value)
}
}

if (getTxRpc) {
cProg = cProg.concat(getTxRpc);
}

const clientOutFile = join(builder.outPath, clientFile);
bundler.writeAst(cProg, clientOutFile);

};
};
6 changes: 3 additions & 3 deletions packages/telescope/src/protod/bufbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { crossGlob as glob } from '../utils/index';

export function parseBufLockFile(filePath: string): Repo[] {
if (!isPathExist(filePath)) {
Expand Down Expand Up @@ -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/**`,
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope/src/protod/recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { crossGlob as glob } from '../utils/index';

export async function cloneAll({
repos,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope/src/protod/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { crossGlob as glob } from '../utils/index';

export function exec(command: string, verbose = false) {
const { code, stdout, stderr } = _exec(command);
Expand Down Expand Up @@ -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/**`,
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/telescope/src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import minimatch from 'minimatch';
import generate from '@babel/generator';
import { unused } from './unused';
import traverse from '@babel/traverse';
import { toPosixPath } from './index';

export const writeAstToFile = (
outPath: string,
Expand All @@ -28,9 +29,9 @@ export const writeAstToFile = (
});
traverse(newAst, unused);
const content2 = generate(newAst).code;
writeContentToFile(outPath, options, content2, filename);
writeContentToFile(toPosixPath(outPath), options, content2, toPosixPath(filename));
} else {
writeContentToFile(outPath, options, content, filename);
writeContentToFile(toPosixPath(outPath), options, content, toPosixPath(filename));
}
}

Expand Down
18 changes: 17 additions & 1 deletion packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ 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';

const posixPath = require('path').posix;

export const getRoot = (ref: ProtoRef): ProtoRoot => {
if (ref.traversed) return ref.traversed;
Expand Down Expand Up @@ -107,10 +110,11 @@ export const UTIL_HELPERS = [

export const fixlocalpaths = (imports: ImportObj[]) => {
return imports.map(imp => {
const fixedPath = toPosixPath(imp.path)
return {
...imp,
path: (UTIL_HELPERS.includes(imp.path) || imp.path.startsWith('.') || imp.path.startsWith('@')) ?
imp.path : `./${imp.path}`
fixedPath : `./${fixedPath}`
};
})
};
Expand All @@ -120,7 +124,19 @@ export const getRelativePath = (f1: string, f2: string, ext?: string) => {
const rel = relative(dirname(f1), f2);
let importPath = rel.replace(extname(rel), '');
if (!/^\./.test(importPath)) importPath = `./${importPath}`;
importPath = toPosixPath(importPath)
return restoreExtension(importPath, ext);
}

// Replace all \\ to / for windows support purpose
export const crossGlob = (input:string, options?:object) => {
return globSync(toPosixPath(input), options);
}

// Unify all the path to posixPath for windows support purpose
export const toPosixPath = (mixedPath): string => {
return mixedPath.replace(/\\/g, posixPath.sep);
}


export * from './common-create-bundle';
4 changes: 2 additions & 2 deletions packages/telescope/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit bd06b14

Please sign in to comment.