Skip to content

Commit

Permalink
Merge pull request #3 from alichry/fixes/core-v0.1.1_sst-v0.1.1_cli-v…
Browse files Browse the repository at this point in the history
…0.1.2

Fix incompatibility with SST v1 - v1.18.4, AWS-AppSync 2.1-alpha.0 - 2.54-alpha.0
  • Loading branch information
alichry authored Feb 16, 2025
2 parents 19056cb + e776129 commit 831fd14
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 96 deletions.
67 changes: 56 additions & 11 deletions packages/cli/lib/index.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { execa } from 'execa';
import { mkdir as fsMkdir, appendFile as fsAppendFile } from 'fs/promises';
import { mkdir as fsMkdir, appendFile as fsAppendFile, readFile as fsReadFile, writeFile as fsWriteFile } from 'fs/promises';
import { resolve } from 'path';
import { existsSync } from 'node:fs';
import { join } from 'node:path';

const cdkVersion = '2.54.0'; /* AppSync Butler v0.1.1 supports 2.1.0 <= aws-cdk <= 2.54 */
const sstVersion = 'one'; /* SST V2 removes the AppSyncApi export from @serverless-stack/resources */

/**
* @param {string} directory
* @param {string[]} toolkitArgs
* @param {string[]} toolkitArgs
*/
export const initCdk = async (directory, toolkitArgs) => {
await exec(
'💔 An error has occurred when installing CDK.',
'npx', ['aws-cdk', 'init', 'app'].concat(toolkitArgs),
'npx',
[
`aws-cdk@${cdkVersion}`,
'init',
'app'
].concat(toolkitArgs),
directory
);
await exec(
'💔 An error has occurred when installing \'@aws-cdk/aws-appsync-alpha\'',
'npm', ['install', '@aws-cdk/aws-appsync-alpha'],
`💔 An error has occurred when installing \'@aws-cdk/aws-appsync-alpha@${cdkVersion}-alpha.0\'`,
'npm',
[
'install',
`@aws-cdk/aws-appsync-alpha@${cdkVersion}-alpha.0`
],
directory
);
}
Expand All @@ -32,22 +46,43 @@ export const initSst = async (directory, toolkitArgs = []) => {
}
await exec(
'💔 An error has occurred when installing SST.',
'npx', ['create-sst'].concat(toolkitArgs)
'npx', [`create-sst@${sstVersion}`].concat(toolkitArgs)
);
await exec(
'💔 An error has occurred when installing SST.',
'npm', ['install'],
directory
);
// @aws-cdk/aws-appsync-alpha should be already installed by SST,
// but it is a @serverless-stack/resources dependency. Let's install it
// as a root dependency to help npm notice that the required
// @aws-cdk/aws-appsync-alpha peer dependency is installed.
await exec(
'💔 An error has occurred when installing \'@aws-cdk/aws-appsync-alpha\'',
'npm', ['exec', '--package=@serverless-stack/cli', 'sst', 'add-cdk', '@aws-cdk/aws-appsync-alpha'],
'npm',
[
'exec',
`--package=@serverless-stack/cli`,
'sst',
'add-cdk',
'@aws-cdk/aws-appsync-alpha'
],
directory
);

const tsconfigPath = join(directory, "tsconfig.json");
if (existsSync(tsconfigPath)) {
// Fix bug in the created typescript project.
// Sadly create-sst V1 is not working out of the box.
// Otherwise, without this fix, TS will emit the below error:
// Cannot find module '@serverless-stack/resources' or its corresponding type declarations

console.log("\n⚠️ Setting \"type\" to \"module\" in package.json");
await replaceInFile(
join(directory, "package.json"),
(content) => {
const obj = JSON.parse(content);
obj.type = "module";
return JSON.stringify(obj, null, 4);
}
);
}
}

/**
Expand Down Expand Up @@ -121,4 +156,14 @@ export const mkdir = async (...paths) => {
export const appendToFile = async (path, str) => {
console.log("Writing to file:", path);
await fsAppendFile(path, str, { encoding: 'ascii', mode: 0o644 });
}

const replaceInFile = async (path, cb) => {
let content = await fsReadFile(path, "ascii");
content = cb(content);
await fsWriteFile(
path,
content,
"ascii"
);
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appsync-butler",
"version": "0.1.1",
"version": "0.1.2",
"description": "CLI to install and manage AppSync Butler",
"keywords": [
"appsync-butler",
Expand Down
31 changes: 0 additions & 31 deletions packages/core/.eslintrc.json

This file was deleted.

6 changes: 6 additions & 0 deletions packages/core/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": ["ts"],
"spec": "test/",
"recursive": true,
"loader": "ts-node/esm"
}
47 changes: 47 additions & 0 deletions packages/core/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import globals from "globals";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [...compat.extends(
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
), {
plugins: {
"@typescript-eslint": typescriptEslint
},

languageOptions: {
globals: {
...globals.node,
},

parser: tsParser,
ecmaVersion: 13,
sourceType: "module",
},

rules: {
indent: ["error", 4],
"no-console": 1,
},
}, {
files: ["test/*.ts"],

rules: {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-expressions": "off"
},
}];
42 changes: 22 additions & 20 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@appsync-butler/core",
"version": "0.1.0",
"version": "0.1.1",
"description": "A simple yet powerful AppSync development framework. Compatible with CDK and SST. 🚀 Highly-suitable for Single Table design applications.",
"author": "Ali Cherry",
"homepage": "https://github.com/alichry/appsync-butler#readme",
Expand All @@ -24,39 +24,41 @@
"publishConfig": {
"access": "public"
},
"engines" : {
"node" : ">=16.0.0 <25.0.0"
},
"scripts": {
"lint": "eslint . --ext .ts",
"lint": "eslint **/*.ts",
"pretranspile:esm": "rm -rf ./dist/esm",
"pretranspile:cjs": "rm -rf ./dist/cjs",
"transpile:esm": "tsc --project tsconfig.build.json --outDir ./dist/esm",
"transpile:cjs": "tsc --project tsconfig.build.cjs.json --outDir ./dist/cjs",
"posttranspile:cjs": "echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
"transpile": "npm run transpile:esm && npm run transpile:cjs",
"build": "npm run transpile",
"test": "c8 mocha --loader=ts-node/esm 'test/**/*.{ts,js}'",
"test": "c8 mocha",
"prepublishOnly": "npm run build && npm run test"
},
"devDependencies": {
"@aws-cdk/aws-appsync-alpha": "2.24.0-alpha.0",
"@types/chai": "^4.3.0",
"@types/lodash": "^4.14.178",
"@types/mocha": "^9.1.1",
"@types/node": "^16.11.12",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"aws-cdk-lib": "2.24.0",
"c8": "^7.11.3",
"chai": "^4.3.4",
"eslint": "^8.4.1",
"eslint-plugin-import": "^2.25.3",
"@aws-cdk/aws-appsync-alpha": "2.50.0-alpha.0",
"@types/chai": "^5.0.1",
"@types/lodash": "^4.17.15",
"@types/mocha": "^10.0.10",
"@types/node": "^22.13.4",
"@typescript-eslint/eslint-plugin": "^8.24.0",
"aws-cdk-lib": "2.50.0",
"c8": "^10.1.3",
"chai": "^5.2.0",
"eslint": "^9.20.1",
"eslint-plugin-import": "^2.31.0",
"lodash": "^4.17.21",
"mocha": "^9.1.3",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
"mocha": "^11.1.0",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
},
"peerDependencies": {
"@aws-cdk/aws-appsync-alpha": "*",
"aws-cdk-lib": "2.x"
"@aws-cdk/aws-appsync-alpha": "2.0.0-alpha.1 || 2.0.0-alpha.2 || 2.0.0-alpha.3 || 2.0.0-alpha.4 || 2.0.0-alpha.5 || 2.0.0-alpha.6 || 2.0.0-alpha.7 || 2.0.0-alpha.8 || 2.0.0-alpha.9 || 2.0.0-alpha.10 || 2.0.0-alpha.11 || 2.0.0-rc.23 || 2.0.0-rc.24 || 2.2.0-alpha.0 || 2.3.0-alpha.0 || 2.4.0-alpha.0 || 2.5.0-alpha.0 || 2.6.0-alpha.0 || 2.7.0-alpha.0 || 2.8.0-alpha.0 || 2.9.0-alpha.0 || 2.10.0-alpha.0 || 2.11.0-alpha.0 || 2.12.0-alpha.0 || 2.13.0-alpha.0 || 2.14.0-alpha.0 || 2.15.0-alpha.0 || 2.16.0-alpha.0 || 2.17.0-alpha.0 || 2.18.0-alpha.0 || 2.19.0-alpha.0 || 2.20.0-alpha.0 || 2.21.0-alpha.0 || 2.21.1-alpha.0 || 2.22.0-alpha.0 || 2.23.0-alpha.0 || 2.24.0-alpha.0 || 2.24.1-alpha.0 || 2.25.0-alpha.0 || 2.26.0-alpha.0 || 2.27.0-alpha.0 || 2.28.0-alpha.0 || 2.28.1-alpha.0 || 2.29.0-alpha.0 || 2.29.1-alpha.0 || 2.30.0-alpha.0 || 2.31.0-alpha.0 || 2.31.1-alpha.0 || 2.31.2-alpha.0 || 2.32.0-alpha.0 || 2.32.1-alpha.0 || 2.33.0-alpha.0 || 2.34.0-alpha.0 || 2.34.1-alpha.0 || 2.34.2-alpha.0 || 2.35.0-alpha.0 || 2.36.0-alpha.0 || 2.37.0-alpha.0 || 2.37.1-alpha.0 || 2.38.0-alpha.0 || 2.38.1-alpha.0 || 2.39.0-alpha.0 || 2.39.1-alpha.0 || 2.40.0-alpha.0 || 2.41.0-alpha.0 || 2.42.0-alpha.0 || 2.42.1-alpha.0 || 2.43.0-alpha.0 || 2.43.1-alpha.0 || 2.44.0-alpha.0 || 2.45.0-alpha.0 || 2.46.0-alpha.0 || 2.47.0-alpha.0 || 2.48.0-alpha.0 || 2.49.0-alpha.0 || 2.49.1-alpha.0 || 2.50.0-alpha.0 || 2.51.0-alpha.0 || 2.51.1-alpha.0 || 2.52.0-alpha.0 || 2.52.1-alpha.0 || 2.53.0-alpha.0 || 2.54.0-alpha.0",
"aws-cdk-lib": ">= 2.1.0 <= 2.54.0"
},
"keywords": [
"appsync-butler",
Expand Down
53 changes: 53 additions & 0 deletions packages/core/test/test-appsync-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

set -e

versions="$(npm view @aws-cdk/aws-appsync-alpha versions --json \
| sed -E '/^\[|\]/d;s/.*\"(.*)\".*$/\1/g' )"

good=""
bad=""
inconclusive=""

for v in $versions
do
if ! npm install -D "@aws-cdk/aws-appsync-alpha@$v" "aws-cdk-lib@*"; then
echo "⚠️ failed to install $v"
if [ -z "$inconclusive" ]; then
inconclusive="$v"
else
inconclusive="$inconclusive || $v"
fi
fi
if npm run build && npx mocha; then
echo "$v"
if [ -z "$good" ]; then
good="$v"
else
good="$good || $v"
fi
else
echo "$v"
if [ -z "$bad" ]; then
bad="$v"
else
bad="$bad || $v"
fi
fi
done

echo "⚠️ Could not test the below versions: "

echo "$inconclusive"

echo ""

echo "✅ Tests passed for the below versions: "

echo "$good"

echo ""

echo "❌ And failedd for the below versions: "

echo "$bad"
4 changes: 3 additions & 1 deletion packages/core/test/utilts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const loadCdk = (opts: Omit<LoaderOptions, 'api' | 'dataSources'>, noDefa
const secondaryTable = new Table(dummyStack, "dummyTable2", {
partitionKey: { name: "pk", type: AttributeType.STRING }
});
const api = new GraphqlApi(dummyStack, "appsync", { name: "dummyApi" });
const api = new GraphqlApi(dummyStack, "appsync", {
name: "dummyApi"
});
const ds = api.addDynamoDbDataSource("myTable", table);
const secondaryDs = api.addDynamoDbDataSource("test", secondaryTable);
const loader = new Loader(dummyStack, {
Expand Down
6 changes: 6 additions & 0 deletions packages/sst/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": ["ts"],
"spec": "test/",
"recursive": true,
"loader": "ts-node/esm"
}
2 changes: 1 addition & 1 deletion packages/sst/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function MyStack({ stack }: StackContext) {
});

const loader = new SstLoader(stack, {
sstApi: api,
api,
defaultUnitResolverDataSource: 'tableDs',
defaultFunctionDataSource: 'tableDs'
});
Expand Down
3 changes: 3 additions & 0 deletions packages/sst/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import defaultConfig from "../core/eslint.config.mjs";

export default defaultConfig;
4 changes: 2 additions & 2 deletions packages/sst/lib/SstLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default class SstLoader extends Loader {
public readonly defaultFunctionDs?: string | 'none';

constructor(scope: Loader['scope'], options: SstLoaderOptions) {
super(scope, { ...options, api: options.sstApi.cdk.graphqlApi });
this.sstApi = options.sstApi;
super(scope, { ...options, api: options.api.cdk.graphqlApi });
this.sstApi = options.api;
}

protected resolveStringDataSource(
Expand Down
3 changes: 1 addition & 2 deletions packages/sst/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { AppSyncApi as SstAppSyncApi } from '@serverless-stack/resources';
import { ParserOptions } from '@appsync-butler/core';

export interface SstLoaderOptions extends ParserOptions {
sstApi: SstAppSyncApi;
api?: never;
api: SstAppSyncApi;
defaultFunctionDataSource?: string | 'none';
defaultUnitResolverDataSource?: string | 'none';
dataSources?: never;
Expand Down
Loading

0 comments on commit 831fd14

Please sign in to comment.