Skip to content

Commit

Permalink
feat: Update dependencies
Browse files Browse the repository at this point in the history
fix: CLI and ESM
  • Loading branch information
loune committed Dec 30, 2024
1 parent e9d2947 commit 6894c49
Show file tree
Hide file tree
Showing 9 changed files with 2,148 additions and 1,701 deletions.
16 changes: 14 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import globals from 'globals';
import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript';
import importPlugin from 'eslint-plugin-import';

const eslintTsConfig = tseslint.config(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
importPlugin.flatConfigs.recommended,
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
Expand All @@ -26,15 +30,23 @@ const eslintTsConfig = tseslint.config(
'react/prop-types': 'off',

'no-useless-constructor': 'off',
'import/no-unresolved': 'off', // doesn't work with TS ESM
'import/named': 'off', // doesn't work with types
},
}
settings: {
'import/resolver-next': [
createTypeScriptImportResolver({
alwaysTryTypes: true,
}),
],
},
},
);

export default [
...eslintTsConfig,
{
files: ['**/*.test.ts', '**/*.test.tsx'],

languageOptions: {
globals: {
...globals.jest,
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test": "yarn lint && yarn env-cmd --silent jest --detectOpenHandles --coverage",
"lint": "npx eslint src",
"build": "rimraf dist-esm dist-cjs dist-types && yarn tsc -b ./tsconfig.esm.json ./tsconfig.cjs.json ./tsconfig.types.json && chmod +x dist-esm/cli.js && echo '{\"type\": \"commonjs\"}' > ./dist-cjs/package.json",
"prepublish": "yarn build"
"prepublish": "yarn build",
"cli": "yarn build && node dist-esm/cli.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -91,14 +92,16 @@
"env-cmd": "^10.1.0",
"eslint": "^9.6.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.7.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.1.2",
"globals": "^15.8.0",
"jest": "^29.5.0",
"prettier": "^3.1.1",
"prettier": "^3.4.2",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4",
"typescript-eslint": "^7.15.0"
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.2"
},
"dependencies": {
"fast-glob": "^3.2.4",
Expand Down
2 changes: 1 addition & 1 deletion src/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test('azure uploadFile', async () => {
resolve(Buffer.concat(buffers).toString());
});
downloadBlockBlobResponse.readableStreamBody?.on('error', (err) => {
reject(err);
reject(err as Error);
});
});
expect(streamString).toBe(fs.readFileSync(testFile).toString());
Expand Down
66 changes: 32 additions & 34 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import yargs from 'yargs';
import yargs from 'yargs/yargs';
import push from './push.js';
import { UploadFileProvider } from './types.js';

Expand All @@ -16,7 +16,7 @@ interface Argv {

type UploadFileProviderConstructor = (options: unknown) => UploadFileProvider;

function getProvider(argv: Argv): UploadFileProvider {
async function getProvider(argv: Argv): Promise<UploadFileProvider> {
const [, proto, bucket] = /^([a-zA-Z0-9]+):\/\/([a-zA-Z0-9-.]+)\/*/.exec(argv.destination) ?? [null, null, null];

if (proto === null) {
Expand All @@ -27,29 +27,26 @@ function getProvider(argv: Argv): UploadFileProvider {

if (proto === 's3') {
const providerOptions = { bucket, listMetaDataConcurrency: argv.concurrency };
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const s3FileProvider = require('./s3').default as UploadFileProviderConstructor;
const s3FileProvider = (await import('./s3.js')).default as UploadFileProviderConstructor;
return s3FileProvider(providerOptions);
}

if (proto === 'gcp') {
const providerOptions = { bucket };
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const gcpProvider = require('./gcp').default as UploadFileProviderConstructor;
const gcpProvider = (await import('./gcp.js')).default as UploadFileProviderConstructor;
return gcpProvider(providerOptions);
}

if (proto === 'azure') {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const { SharedKeyCredential } = require('@azure/storage-blob');
const { StorageSharedKeyCredential } = await import('@azure/storage-blob');
const { accountName, accountKey } = argv;

const providerOptions = {
account: argv.accountName,
account: accountName,
containerName: bucket,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
credential: argv.accountName ? new SharedKeyCredential(argv.accountName, argv.accountKey) : undefined,
credential: accountName && accountKey ? new StorageSharedKeyCredential(accountName, accountKey) : undefined,
};
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const gcpProvider = require('./azure').default as UploadFileProviderConstructor;
const gcpProvider = (await import('./azure.js')).default as UploadFileProviderConstructor;
return gcpProvider(providerOptions);
}

Expand All @@ -68,7 +65,8 @@ const logger = {
},
};

const result = yargs
const result = yargs(process.argv.slice(2))
.scriptName('snap-push')
.command<Argv>(
'* <source> <destination>',
'Push files to the remote file service.',
Expand All @@ -80,28 +78,28 @@ const result = yargs
describe: 'destination bucket',
});
},
(argv) => {
async (argv) => {
const startTime = Date.now();
// act
push({
files: argv.source.split(','),
provider: getProvider(argv),
destPathPrefix: argv.prefix,
logger,
concurrency: argv.concurrency,
makePublic: argv.public,
onlyUploadChanges: !argv.force,
})
.then((result) => {
logger.info(
`Finished in ${Math.round((Date.now() - startTime) / 1000)}s. (Uploaded ${
result.uploadedKeys.length
}. Deleted ${result.deletedKeys.length}. Skipped ${result.skippedKeys.length}.)`,
);
})
.catch((error) => {
logger.error(`Error: ${error}`);
try {
// act
const result = await push({
files: argv.source.split(','),
provider: await getProvider(argv),
destPathPrefix: argv.prefix,
logger,
concurrency: argv.concurrency,
makePublic: argv.public,
onlyUploadChanges: !argv.force,
});

logger.info(
`Finished in ${Math.round((Date.now() - startTime) / 1000)}s. (Uploaded ${
result.uploadedKeys.length
}. Deleted ${result.deletedKeys.length}. Skipped ${result.skippedKeys.length}.)`,
);
} catch (error: any) {
logger.error(`Error: ${error}`);
}
},
)
.option('concurrency', {
Expand Down
6 changes: 3 additions & 3 deletions src/contentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async function readChars(filename: string, numOfChars: number): Promise<string>

export default async function getFileMimeType(
filename: string,
customMimeTypes?: Record<string, string[]> | undefined,
customMimeTypes?: Record<string, string[]>,
): Promise<string | undefined> {
const extension = filename.substring(filename.lastIndexOf('.') + 1);

Expand All @@ -143,11 +143,11 @@ export default async function getFileMimeType(
try {
const chars = await readChars(filename, 200);
const charsLower = chars.toLowerCase();
if (charsLower.indexOf('<html>') !== -1 || charsLower.indexOf('<!doctype html>') !== -1) {
if (charsLower.includes('<html>') || charsLower.includes('<!doctype html>')) {
type = standardExtContentTypeMap.get('html')?.[0];
}
// eslint-disable-next-line no-empty
} catch (err) {}
} catch {}
}

return type;
Expand Down
10 changes: 5 additions & 5 deletions src/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { Storage } from '@google-cloud/storage';
import fg from 'fast-glob';
import { StorageSharedKeyCredential, BlobServiceClient, BlobItem } from '@azure/storage-blob';
import { ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3';
import push, { pathTrimStart } from './push';
import s3FileProvider from './s3';
import azureFileProvider from './azure';
import gcpFileProvider from './gcp';
import { UploadFileProvider, UploadFile, AbstractLogger, UploadArgs } from './types';
import push, { pathTrimStart } from './push.js';
import s3FileProvider from './s3.js';
import azureFileProvider from './azure.js';
import gcpFileProvider from './gcp.js';
import { UploadFileProvider, UploadFile, AbstractLogger, UploadArgs } from './types.js';

const s3TestBucketName = 'pouch-test';

Expand Down
2 changes: 1 addition & 1 deletion src/s3.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
import uploadFileFactory from './s3';
import uploadFileFactory from './s3.js';

jest.setTimeout(10000);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["src"]
"include": ["eslint.config.mjs", "src"]
}
Loading

0 comments on commit 6894c49

Please # to comment.