Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate type declaration files #118

Merged
merged 14 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/soft-books-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@bluecadet/launchpad-dashboard": patch
"@bluecadet/launchpad": patch
"@bluecadet/launchpad-scaffold": patch
"@bluecadet/launchpad-content": patch
"@bluecadet/launchpad-monitor": patch
"@bluecadet/launchpad-utils": patch
---

Generate d.ts declaration files for intellisense, and fix all type errors.
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ jobs:
run: npm install

- name: Run eslint
run: npm run lint
run: npm run lint

- name: Validate types
run: npm run build
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
id: changesets
uses: changesets/action@v1
with:
publish: npx changeset publish
publish: npm run publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,6 @@ dist

# changesets
!.changeset/config.json

# generated .d.ts files
types
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"watch-docs": "nodemon",
"lint": "npx eslint ./packages/**/*.js",
"lint:fix": "npx eslint ./packages/**/*.js --fix",
"lint:fix-dry": "npx eslint ./packages/**/*.js --fix-dry-run"
"lint:fix-dry": "npx eslint ./packages/**/*.js --fix-dry-run",
"build": "npm run build -w @bluecadet/launchpad",
"release": "npm run build && changeset publish"
},
"repository": {
"type": "git",
Expand All @@ -34,9 +36,11 @@
"devDependencies": {
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.23.0",
"@types/node": "^20.3.1",
"eslint": "^8.32.0",
"eslint-config-standard": "^17.0.0",
"jsdoc-to-markdown": "^7.1.1",
"nodemon": "^2.0.20"
"nodemon": "^2.0.20",
"typescript": "^5.1.3"
}
}
5 changes: 4 additions & 1 deletion packages/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export * from './lib/launchpad-content.js';
export * from './lib/utils/file-utils.js';
export default LaunchpadContent;

/**
* @param {import('./lib/content-options.js').ContentOptions | {content: import('./lib/content-options.js').ContentOptions}} config
*/
export const launch = async (config) => {
const content = new LaunchpadContent(config.content || config);
const content = new LaunchpadContent('content' in config ? config.content : config);
await content.start();
};

Expand Down
9 changes: 6 additions & 3 deletions packages/content/lib/content-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class ContentOptions {
return '%TIMESTAMP%';
}

/**
* @param {any} options
*/
constructor({
sources = [],
imageTransforms = [],
Expand Down Expand Up @@ -77,21 +80,21 @@ export class ContentOptions {

/**
* Temp file directory path.
* @type {boolean}
* @type {string}
* @default '%DOWNLOAD_PATH%/.tmp/'
*/
this.tempPath = tempPath;

/**
* Temp directory path where all downloaded content will be backed up before removal.
* @type {boolean}
* @type {string}
* @default '%DOWNLOAD_PATH%/.backups/'
*/
this.backupPath = backupPath;

/**
* Which files to keep in `dest` if `clearOldFilesOnSuccess` or `clearOldFilesOnStart` are `true`. E.g. `'*.json|*.csv|*.xml|*.git*'`
* @type {boolean}
* @type {string}
* @default ''
*/
this.keep = keep;
Expand Down
53 changes: 42 additions & 11 deletions packages/content/lib/content-sources/airtable-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Credentials from '../credentials.js';
* Options for AirtableSource
*/
export class AirtableOptions extends SourceOptions {
/**
* @param {any} options
*/
constructor({
baseId = undefined,
tables = [],
Expand All @@ -31,6 +34,7 @@ export class AirtableOptions extends SourceOptions {
this.baseId = baseId;

/**
* The table view which to select for syncing by default
* @type {string}
* @default 'Grid view'
*/
Expand Down Expand Up @@ -58,12 +62,6 @@ export class AirtableOptions extends SourceOptions {
*/
this.endpointUrl = endpointUrl;

/**
* The table view which to select for syncing by default
* @type {string}
*/
this.defaultView = defaultView;

/**
* Appends the local path of attachments to the saved JSON
* @type {boolean}
Expand All @@ -73,12 +71,23 @@ export class AirtableOptions extends SourceOptions {
}
}

/**
* @extends {ContentSource<AirtableOptions>}
*/
export class AirtableSource extends ContentSource {
/** @type {Airtable.Base} */
_base = null;
/**
* @type {Airtable.Base}
*/
_base;

/**
* @type {Record<string, Airtable.Record<Airtable.FieldSet>[]>}
*/
_rawAirtableData = {};

/**
* @type {Record<string, unknown[]>}
*/
_simplifiedData = {};

/**
Expand Down Expand Up @@ -135,14 +144,17 @@ export class AirtableSource extends ContentSource {
* @param {string} tableId
* @param {boolean} isKeyValueTable
* @param {ContentResult} result
* @returns {ContentResult}
* @returns {Promise<ContentResult>}
*/
async _processTable(tableId, isKeyValueTable = false, result = new ContentResult()) {
// Write raw Data file.

const rawDataPath = `${tableId}.raw.json`;
result.addDataFile(rawDataPath, this._rawAirtableData[tableId]);

/**
* @type {any}
*/
const simpData = isKeyValueTable ? {} : [];
this._simplifiedData[tableId] = [];

Expand Down Expand Up @@ -198,15 +210,27 @@ export class AirtableSource extends ContentSource {
return result;
}

/**
* @param {string} str
* @returns {boolean}
*/
_isBoolStr(str) {
return str === 'true' || str === 'false';
}

/**
* @param {string} str
* @returns {boolean}
*/
_isNumericStr(str) {
return !isNaN(str);
return !isNaN(Number(str));
}

// Get Data.
/**
* @param {string} table
* @param {boolean} force
*/
async _getData(table, force = false) {
// If force, clear the data.
if (force) {
Expand All @@ -224,9 +248,16 @@ export class AirtableSource extends ContentSource {
});
}

// Fetch from Airtable.
/**
* Fetch from Airtable.
* @param {string} table table name
* @returns {Promise<Airtable.Record<Airtable.FieldSet>[]>}
*/
async _fetchData(table) {
return new Promise((resolve, reject) => {
/**
* @type {Airtable.Record<Airtable.FieldSet>[]}
*/
const rows = [];

this._base(table)
Expand Down
24 changes: 17 additions & 7 deletions packages/content/lib/content-sources/content-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DataFile {
/**
*
* @param {string} localPath
* @param {string|JSON|Object|Array} content
* @param {string|JSON|object|Array<any>} content
*/
constructor(localPath, content) {
this.localPath = localPath;
Expand All @@ -35,11 +35,16 @@ export class DataFile {
}
}
export class MediaDownload {
/**
* @param {object} options
* @param {string} options.url
* @param {string} [options.localPath]
*/
constructor({
url,
localPath = undefined,
...rest
} = {}) {
}) {
/**
* The url to download
* @type {string}
Expand Down Expand Up @@ -68,7 +73,7 @@ export class MediaDownload {
export class ContentResult {
/**
* List of data files to save
* @type {Array<ContentResult>}
* @param {Array<ContentResult>} results
*/
static combine(results) {
const finalResult = results.reduce((previousValue, currentValue) => {
Expand Down Expand Up @@ -120,10 +125,10 @@ export class ContentResult {

/**
*
* @param {MediaDownload} urlOrDownload
* @param {MediaDownload | string} urlOrDownload
*/
addMediaDownload(urlOrDownload) {
if (typeof urlOrDownload === 'string' || urlOrDownload instanceof String) {
if (typeof urlOrDownload === 'string') {
urlOrDownload = new MediaDownload({
url: urlOrDownload
});
Expand All @@ -133,7 +138,7 @@ export class ContentResult {

/**
*
* @param {Iterable} files
* @param {Iterable<MediaDownload>} files
*/
addMediaDownloads(files) {
this.mediaDownloads.push(...files);
Expand All @@ -144,10 +149,15 @@ export class ContentResult {
* @param {string} id
*/
collate(id) {
/**
* @type {Array<DataFile>}
*/
const initial = [];

// Collect all data into 1 object.
const collatedData = this.dataFiles.reduce((previousValue, currentValue) => {
return [...previousValue, ...currentValue.content];
}, []);
}, initial);

// Remove old datafiles.
this.dataFiles = [];
Expand Down
26 changes: 14 additions & 12 deletions packages/content/lib/content-sources/content-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ export class SourceOptions {
}
}

/**
* @template {SourceOptions} [C=SourceOptions]
*/
export class ContentSource {
/** @type {SourceOptions} */
config = null;
/** @type {Logger} */
logger = null;
/** @type {C} */
config;
/** @type {Logger | Console} */
logger;

/**
* @param {SourceOptions} config Content source options. `id` is a required field.
* @param {Logger} logger The logger to use for all output. Defaults to console.
* @param {C} config Content source options. `id` is a required field.
* @param {Logger} [logger] The logger to use for all output. Defaults to console.
* @throws {Error} Throws an error if no `config` or `config.id` is defined.
*/
constructor(config, logger = console) {
this.logger = logger;
constructor(config, logger) {
this.logger = logger ?? console;
this.config = config;

if (!this.config || !this.config.id) {
Expand All @@ -64,17 +67,16 @@ export class ContentSource {
* @returns {Promise<ContentResult>} that resolves only when all content has been downloaded successfully
*/
async fetchContent() {
this.logger.info(chalk.green(`Downloading functionality not implemented for '${chalk.yellow(this.config.id)}'`));
return Promise.resolve();
throw new Error(`Downloading functionality not implemented for '${chalk.yellow(this.config.id)}'`);
}

/**
* Removes all content and media files in the temp and dest directories (temp first, then dest).
*
* @returns {Promise}
* @returns {Promise<void>}
*/
async clearContent() {
await fs.remove(this.config);
throw new Error('clearContent not implemented');
}

toString() {
Expand Down
Loading