Skip to content

Commit

Permalink
fix: fix for fs in pkg destined for node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
MirandaWood committed Jan 16, 2023
1 parent 42d2104 commit feeb261
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// Q: how are we merging mapping key and ownerPK in edge case?
// Q: should we reduce constraints a mapping's commitment's preimage by not having the extra inner hash? Not at the moment, because it adds complexity to transpilation.
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const testReadPath = './src/boilerplate/common/generic-test.mjs';
const apiServiceReadPath = './src/boilerplate/common/services/generic-api_services.mjs';
const apiRoutesReadPath = './src/boilerplate/common/routes/generic-api_routes.mjs';
const testReadPath = path.resolve(fileURLToPath(import.meta.url), '../../../../../../src/boilerplate/common/generic-test.mjs');
const pathPrefix = path.resolve(fileURLToPath(import.meta.url), '../../../../../../src/boilerplate/common/');
class BoilerplateGenerator {
generateBoilerplate(node: any, fields: any = {}) {
const { bpSection, bpType, ...otherParams } = node;
Expand Down Expand Up @@ -663,57 +664,57 @@ integrationApiRoutesBoilerplate = {
zappFilesBoilerplate = () => {
return [
{
readPath: 'src/boilerplate/common/bin/startup',
readPath: pathPrefix + '/bin/startup',
writePath: '/bin/startup',
generic: true,
},
{
readPath: 'src/boilerplate/common/config/default.js',
readPath: pathPrefix + '/config/default.js',
writePath: '/config/default.js',
generic: false,
},
{
readPath: 'src/boilerplate/common/migrations/1_initial_migration.js',
readPath: pathPrefix + '/migrations/1_initial_migration.js',
writePath: 'migrations/1_initial_migration.js',
generic: true,
},
{
readPath: 'src/boilerplate/common/boilerplate-package.json',
readPath: pathPrefix + '/boilerplate-package.json',
writePath: './package.json',
generic: true,
},
{
readPath: 'src/boilerplate/common/boilerplate-docker-compose.yml',
readPath: pathPrefix + '/boilerplate-docker-compose.yml',
writePath: './docker-compose.zapp.yml',
generic: true,
},
{
readPath: 'src/boilerplate/common/boilerplate-Dockerfile',
readPath: pathPrefix + '/boilerplate-Dockerfile',
writePath: './Dockerfile',
generic: true,
},
{
readPath: 'src/boilerplate/common/boilerplate-Dockerfile.deployer',
readPath: pathPrefix + '/boilerplate-Dockerfile.deployer',
writePath: './Dockerfile.deployer',
generic: true,
},
{
readPath: 'src/boilerplate/common/boilerplate-Dockerfile.mongo',
readPath: pathPrefix + '/boilerplate-Dockerfile.mongo',
writePath: './Dockerfile.mongo',
generic: true,
},
{
readPath: 'src/boilerplate/common/setup-admin-user.js',
readPath: pathPrefix + '/setup-admin-user.js',
writePath: './setup-admin-user.js',
generic: true,
},
{
readPath: 'src/boilerplate/common/entrypoint.sh',
readPath: pathPrefix + '/entrypoint.sh',
writePath: './entrypoint.sh',
generic: true,
},
{
readPath: 'src/boilerplate/common/truffle-config.js',
readPath: pathPrefix + '/truffle-config.js',
writePath: './truffle-config.js',
generic: true,
},
Expand Down
3 changes: 2 additions & 1 deletion src/codeGenerators/circuit/zokrates/toCircuit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable import/no-cycle, no-nested-ternary */
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { collectImportFiles } from '../../common.js'
import CircuitBP from '../../../boilerplate/circuit/zokrates/raw/BoilerplateGenerator.js';
import NodePath from '../../../traverse/NodePath.js'
Expand All @@ -20,7 +21,7 @@ function codeGenerator(node: any) {
file,
};
if (!file && node.fileName === `joinCommitments`) {
thisFile.file = fs.readFileSync('./circuits/common/joinCommitments.zok', 'utf8');
thisFile.file = fs.readFileSync(path.resolve(fileURLToPath(import.meta.url), '../../../../../circuits/common/joinCommitments.zok'), 'utf8');
}
const importedFiles = collectImportFiles(thisFile.file, 'circuit');
return [thisFile, ...importedFiles];
Expand Down
16 changes: 9 additions & 7 deletions src/codeGenerators/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

export interface localFile {
filepath: string,
Expand Down Expand Up @@ -49,7 +50,7 @@ export const collectImportFiles = (
let localFilePaths: string[];
switch (context) {
case 'circuit': {
contextDirPath ??= './circuits';
contextDirPath ??= path.resolve(fileURLToPath(import.meta.url), '../../../circuits/');
localFilePaths = ImportStatementList.reduce((acc: string[], line: string) => {
let importFilePath = line.match(/"(.*?)"/g)[0].replace(/"/g, ''); // get text between quotes; i.e. the import filepaths
importFilePath += path.extname(importFilePath) === '.zok' ? '' : '.zok'; // ensure file extension.
Expand All @@ -69,7 +70,7 @@ export const collectImportFiles = (
break;
}
case 'contract': {
contextDirPath ??= './contracts';
contextDirPath ??= path.resolve(fileURLToPath(import.meta.url), '../../../contracts/');
localFilePaths = ImportStatementList.reduce((acc: string[], line: string) => {
let importFilePath = line.match(/"(.*?)"/g)[0].replace(/"/g, ''); // get text between quotes; i.e. the import filepaths
importFilePath += path.extname(importFilePath) === '.sol' ? '' : '.sol'; // ensure file extension.
Expand All @@ -79,7 +80,7 @@ export const collectImportFiles = (
break;
}
case 'orchestration': {
contextDirPath ??= './src/boilerplate/';
contextDirPath ??= path.resolve(fileURLToPath(import.meta.url), './../../../src/boilerplate/');
localFilePaths = ImportStatementList.reduce((acc, line) => {
const importFilePath = line.match(/'(.*?)'/g)[0].replace(/'/g, ''); // get text between quotes; i.e. the import filepaths
acc.push(importFilePath);
Expand All @@ -101,10 +102,11 @@ export const collectImportFiles = (
if (!exists) continue;
const f = fs.readFileSync(relPath, 'utf8');
const n = path.basename(absPath, path.extname(absPath));
const shortRelPath = path.relative(path.resolve(fileURLToPath(import.meta.url), '../../../'), absPath);
const writePath = context === 'orchestration' ? path.join(
'orchestration',
path.relative('./src/boilerplate', relPath),
) : relPath;
'orchestration',
path.relative('./src/boilerplate', shortRelPath)
) : shortRelPath;
if (context === 'contract') {
// if import is an interface, we need to deploy contract e.g. IERC20 -> deploy ERC20
if (
Expand All @@ -115,7 +117,7 @@ export const collectImportFiles = (
// if we import an interface, we must find the original contract
// we assume that any interface begins with I (substring(1)) and the remaining chars are the original contract name
const newLocalPath = p.replace(n, n.substring(1));
const newPath = relPath.replace(n, n.substring(1));
const newPath = shortRelPath.replace(n, n.substring(1));
const check = fs.existsSync(newPath);
if (check) {
localFilePaths.push(newLocalPath);
Expand Down
4 changes: 3 additions & 1 deletion src/codeGenerators/orchestration/files/toOrchestration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable import/no-cycle, no-param-reassign */
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import { collectImportFiles, localFile } from '../../common.js'
import OrchestrationBP from '../../../boilerplate/orchestration/javascript/raw/boilerplate-generator.js';
import codeGenerator from '../nodejs/toOrchestration.js';
Expand Down Expand Up @@ -412,7 +413,8 @@ export default function fileGenerator(node: any) {
'orchestration',
);

const startupScript = { filepath: 'bin/setup', file: fs.readFileSync('src/boilerplate/common/bin/setup', 'utf8')};
const readPath = path.resolve(fileURLToPath(import.meta.url), '../../../../../src/boilerplate/common/bin/setup');
const startupScript = { filepath: 'bin/setup', file: fs.readFileSync(readPath, 'utf8') };
files.push(startupScript);
const vkfile = files.filter(obj => obj.filepath.includes(`write-vk`))[0];
const setupfile = files.filter(obj =>
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"allowJs": true,
"target": "es6",
"moduleResolution": "node",
"module": "es2020",
"esModuleInterop": true,
"baseUrl": ".",
"skipLibCheck": true,
Expand Down

0 comments on commit feeb261

Please sign in to comment.