ember-nativescript-template 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,53 @@
1
- # Temporary Holding Version
1
+ # To-dos, on Ember and NativeScript
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A native iOS and Android app written as Ember components: real native views, no WebView.
4
+
5
+ ```bash
6
+ ns run ios
7
+ ```
8
+
9
+ ```bash
10
+ ns run android
11
+ ```
12
+
13
+ ```bash
14
+ npm test
15
+ ```
16
+
17
+ ```bash
18
+ npm run typecheck
19
+ ```
20
+
21
+ - `ns run` builds, installs and reloads the app on a simulator, an emulator or a plugged-in phone as you save.
22
+ - `npm test` runs the screens in Node, without a phone (`tests/`).
23
+ - `npm run typecheck` checks the templates with Glint: tags, attributes, events and `{{bind}}` fields.
24
+
25
+ ## What's where
26
+
27
+ ```
28
+ src/
29
+ app.ts the first screen and the services
30
+ app.css NativeScript's CSS: a subset of the web's, sizes without units
31
+ components/Home.gts a field to add a to-do, and the list (NativeList recycles its rows)
32
+ components/TodoRow.gts one row
33
+ components/Todo.gts one to-do: mark it done, delete it
34
+ services/todos.ts the list every screen shares, kept on the phone with ApplicationSettings
35
+ tests/app.test.ts the app in Node: type, tap, open a row, go back
36
+ App_Resources/ icons, splash screens, Info.plist, AndroidManifest.xml
37
+ nativescript.config.ts the app id and the build setup
38
+ ```
39
+
40
+ ## How it fits together
41
+
42
+ - A screen is a component. It gets `@nav` (the `navigator` service) to open another one:
43
+ `this.args.nav.navigate(TodoScreen, { args: { id } })`, then `@nav.back`.
44
+ - Native views are lowercase tags: `<stackLayout>`, `<gridLayout>`, `<label>`, `<textField>`, `<button>`… with
45
+ NativeScript's attributes and events (`{{on 'tap'}}`, `{{on 'returnPress'}}`).
46
+ - `{{bind this 'draft'}}` keeps a field and a text field in step both ways, like `v-model`.
47
+ - Services are Ember services: `@service declare todos: Todos;`, registered in `app.ts`.
48
+ - `{{nativeView this.fn}}` hands a view to a function, for animations or a plugin's API.
49
+
50
+ The ember-nativescript README covers the rest: every tag, modals, tabs, platform blocks, lists with several kinds of
51
+ rows, and plugins: https://github.com/betocantu93/ember-nativescript#readme
52
+
53
+ Don't rename the app's folder to `app`: Xcode fails with "Multiple commands produce …/app.app/app".
@@ -0,0 +1,22 @@
1
+ // Runs once, right after `ns create --template ember-nativescript-template`: gives the app the id the CLI chose,
2
+ // puts .gitignore back (npm leaves it out of packages), then removes itself and tools/.
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ module.exports = function (hookArgs) {
7
+ const root = hookArgs.projectDir;
8
+
9
+ try {
10
+ const config = path.join(root, 'nativescript.config.ts');
11
+ if (hookArgs.appId) {
12
+ const source = fs.readFileSync(config, 'utf8');
13
+ fs.writeFileSync(config, source.replace(/id: '[^']*'/, `id: '${hookArgs.appId}'`));
14
+ }
15
+ fs.copyFileSync(path.join(root, 'tools', 'dot.gitignore'), path.join(root, '.gitignore'));
16
+ } catch (error) {
17
+ console.log(error);
18
+ }
19
+
20
+ fs.rmSync(path.join(root, 'tools'), { recursive: true, force: true });
21
+ fs.rmSync(path.join(root, 'hooks', 'after-createProject'), { recursive: true, force: true });
22
+ };
@@ -0,0 +1,10 @@
1
+ import type { NativeScriptConfig } from '@nativescript/core';
2
+
3
+ export default {
4
+ id: 'org.nativescript.emberapp',
5
+ appPath: 'src',
6
+ appResourcesPath: 'App_Resources',
7
+ bundler: 'vite',
8
+ bundlerConfigPath: 'vite.config.mts',
9
+ android: { v8Flags: '--expose_gc', markingMode: 'none' },
10
+ } as NativeScriptConfig;
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "ember-nativescript-template",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
4
+ "description": "An Ember app on NativeScript: screens as .gts components, a recycling list, a service, Glint, and tests that run in Node.",
5
+ "displayName": "Ember",
6
+ "templateType": "App template",
7
+ "license": "MIT",
8
+ "main": "src/app.ts",
9
+ "type": "module",
10
+ "keywords": [
11
+ "nativescript",
12
+ "template",
13
+ "ember",
14
+ "glimmer",
15
+ "category-general"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/betocantu93/ember-nativescript.git",
20
+ "directory": "template"
21
+ },
22
+ "homepage": "https://github.com/betocantu93/ember-nativescript#readme",
23
+ "files": [
24
+ "src",
25
+ "tests",
26
+ "hooks",
27
+ "tools",
28
+ "nativescript.config.ts",
29
+ "vite.config.mts",
30
+ "vitest.config.mts",
31
+ "tsconfig.json",
32
+ "pnpm-workspace.yaml",
33
+ "README.md"
34
+ ],
35
+ "scripts": {
36
+ "ios": "ns run ios",
37
+ "android": "ns run android",
38
+ "test": "vitest run",
39
+ "typecheck": "ember-tsc --noEmit"
40
+ },
41
+ "dependencies": {
42
+ "@glimmer/component": "^2.2.1",
43
+ "@nativescript/core": "~9.1.4",
44
+ "@valor/nativescript-websockets": "^2.0.2",
45
+ "ember-nativescript": "^0.1.2",
46
+ "ember-source": "~7.4.2"
47
+ },
48
+ "devDependencies": {
49
+ "@glint/ember-tsc": "^1.14.4",
50
+ "@glint/template": "^1.11.1",
51
+ "@nativescript/android": "~9.1.3",
52
+ "@nativescript/ios": "~9.1.1",
53
+ "@nativescript/vite": "^8.0.24",
54
+ "typescript": "~5.9.3",
55
+ "vite": "^8.3.0",
56
+ "vite-plugin-ember-gts": "^0.1.0",
57
+ "vitest": "^5.1.1"
58
+ }
59
+ }
@@ -0,0 +1,6 @@
1
+ # Install scripts pnpm may run: @nativescript/core installs the NativeScript CLI's hooks, esbuild and
2
+ # @parcel/watcher fetch their native binaries.
3
+ onlyBuiltDependencies:
4
+ - '@nativescript/core'
5
+ - '@parcel/watcher'
6
+ - esbuild
package/src/app.css ADDED
@@ -0,0 +1,61 @@
1
+ /* NativeScript CSS: a subset of the web's, in device-independent pixels without units. */
2
+
3
+ .new {
4
+ padding: 12 16;
5
+ }
6
+
7
+ .new textField {
8
+ font-size: 17;
9
+ }
10
+
11
+ .left {
12
+ padding: 0 16 8 16;
13
+ color: #8a8a8e;
14
+ font-size: 13;
15
+ }
16
+
17
+ .todo {
18
+ padding: 14 16;
19
+ }
20
+
21
+ .check {
22
+ width: 28;
23
+ font-size: 18;
24
+ color: #e04e39;
25
+ }
26
+
27
+ .todo-title {
28
+ font-size: 17;
29
+ }
30
+
31
+ .done .todo-title {
32
+ color: #8a8a8e;
33
+ text-decoration: line-through;
34
+ }
35
+
36
+ .detail {
37
+ padding: 24 16;
38
+ }
39
+
40
+ .title {
41
+ font-size: 26;
42
+ font-weight: bold;
43
+ }
44
+
45
+ .status {
46
+ margin: 8 0 24 0;
47
+ color: #8a8a8e;
48
+ }
49
+
50
+ .primary {
51
+ background-color: #e04e39;
52
+ color: white;
53
+ border-radius: 10;
54
+ font-weight: bold;
55
+ margin-top: 8;
56
+ }
57
+
58
+ .danger {
59
+ color: #e04e39;
60
+ margin-top: 8;
61
+ }
package/src/app.ts ADDED
@@ -0,0 +1,8 @@
1
+ // The app's entry: the first screen and the services. Screens get `@nav` to move between them, and services through
2
+ // `@service`, as in any Ember app.
3
+ import { createNativeApp } from 'ember-nativescript';
4
+
5
+ import Home from './components/Home.gts';
6
+ import Todos from './services/todos.ts';
7
+
8
+ createNativeApp({ main: Home, services: { todos: Todos } });
@@ -0,0 +1,43 @@
1
+ // The first screen: a field to add a to-do, and the list. Tapping a row opens it.
2
+ import Component from '@glimmer/component';
3
+ import { tracked } from '@glimmer/tracking';
4
+ import { on } from '@ember/modifier';
5
+ import { service } from '@ember/service';
6
+ import { bind, NativeList, type Navigator } from 'ember-nativescript';
7
+
8
+ import type Todos from '../services/todos.ts';
9
+ import type { Todo } from '../services/todos.ts';
10
+ import TodoScreen from './Todo.gts';
11
+ import TodoRow from './TodoRow.gts';
12
+
13
+ export default class Home extends Component<{ Args: { nav: Navigator } }> {
14
+ @service declare todos: Todos;
15
+ @tracked draft = '';
16
+
17
+ add = (): void => {
18
+ this.todos.add(this.draft);
19
+ this.draft = '';
20
+ };
21
+
22
+ open = (todo: Todo): void => {
23
+ this.args.nav.navigate(TodoScreen, { args: { id: todo.id } });
24
+ };
25
+
26
+ <template>
27
+ <actionBar title='To-dos' />
28
+ <gridLayout rows='auto, auto, *'>
29
+ <gridLayout columns='*, auto' class='new'>
30
+ <textField
31
+ id='draft'
32
+ hint='Something to do'
33
+ returnKeyType='done'
34
+ {{bind this 'draft'}}
35
+ {{on 'returnPress' this.add}}
36
+ />
37
+ <button col='1' id='add' class='primary' {{on 'tap' this.add}}>Add</button>
38
+ </gridLayout>
39
+ <label row='1' id='left' class='left'>{{this.todos.left}} left</label>
40
+ <NativeList row='2' id='list' @items={{this.todos.all}} @row={{TodoRow}} @onItemTap={{this.open}} />
41
+ </gridLayout>
42
+ </template>
43
+ }
@@ -0,0 +1,43 @@
1
+ // One to-do, opened from the list with its id. It reads the to-do from the service, so marking it done shows here and
2
+ // on the list behind it.
3
+ import Component from '@glimmer/component';
4
+ import { on } from '@ember/modifier';
5
+ import { service } from '@ember/service';
6
+ import type { Navigator } from 'ember-nativescript';
7
+
8
+ import type Todos from '../services/todos.ts';
9
+
10
+ export default class TodoScreen extends Component<{ Args: { id: number; nav: Navigator } }> {
11
+ @service declare todos: Todos;
12
+
13
+ get todo() {
14
+ return this.todos.find(this.args.id);
15
+ }
16
+
17
+ toggle = (): void => {
18
+ this.todos.toggle(this.args.id);
19
+ };
20
+
21
+ remove = (): void => {
22
+ this.todos.remove(this.args.id);
23
+ this.args.nav.back();
24
+ };
25
+
26
+ <template>
27
+ <actionBar title='To-do'>
28
+ <navigationButton text='Back' {{on 'tap' @nav.back}} />
29
+ </actionBar>
30
+ {{#if this.todo}}
31
+ <stackLayout class='detail'>
32
+ <label id='title' class='title' textWrap='true' text={{this.todo.title}} />
33
+ <label id='status' class='status'>{{if this.todo.done 'Done' 'To do'}}</label>
34
+ <button id='toggle' class='primary' {{on 'tap' this.toggle}}>{{if
35
+ this.todo.done
36
+ 'Mark as not done'
37
+ 'Mark as done'
38
+ }}</button>
39
+ <button id='remove' class='danger' {{on 'tap' this.remove}}>Delete</button>
40
+ </stackLayout>
41
+ {{/if}}
42
+ </template>
43
+ }
@@ -0,0 +1,11 @@
1
+ // One row of the list. NativeList reuses rows as they scroll, so a row shows `@item` and keeps no state of its own.
2
+ import type { TOC } from '@ember/component/template-only';
3
+
4
+ import type { Todo } from '../services/todos.ts';
5
+
6
+ <template>
7
+ <gridLayout columns='auto, *' class='todo {{if @item.done "done"}}'>
8
+ <label class='check' text={{if @item.done '✓' '○'}} />
9
+ <label col='1' class='todo-title' text={{@item.title}} />
10
+ </gridLayout>
11
+ </template> satisfies TOC<{ Args: { item: Todo; index: number } }>;
package/src/gts.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ // For NativeScript's build-time TypeScript check, which doesn't know .gts files. Glint (`pnpm typecheck`) reads the
2
+ // components themselves.
3
+ declare module '*.gts' {
4
+ const component: object;
5
+ export default component;
6
+ }
@@ -0,0 +1,55 @@
1
+ // The to-do list every screen shares, kept on the phone with ApplicationSettings (a key-value store that survives
2
+ // restarts). Each change replaces the array, so screens that read `all` re-render.
3
+ import { tracked } from '@glimmer/tracking';
4
+ import { ApplicationSettings } from '@nativescript/core';
5
+
6
+ export interface Todo {
7
+ id: number;
8
+ title: string;
9
+ done: boolean;
10
+ }
11
+
12
+ const KEY = 'todos.v1';
13
+
14
+ const FIRST: Todo[] = [
15
+ { id: 1, title: 'Open src/components/Home.gts', done: false },
16
+ { id: 2, title: 'Tap a to-do to open it', done: false },
17
+ { id: 3, title: 'Run the tests: pnpm test', done: false },
18
+ ];
19
+
20
+ export default class Todos {
21
+ @tracked all: Todo[] = load();
22
+
23
+ get left(): number {
24
+ return this.all.filter((todo) => !todo.done).length;
25
+ }
26
+
27
+ find(id: number): Todo | undefined {
28
+ return this.all.find((todo) => todo.id === id);
29
+ }
30
+
31
+ add(title: string): void {
32
+ const trimmed = title.trim();
33
+ if (!trimmed) return;
34
+ const id = Math.max(0, ...this.all.map((todo) => todo.id)) + 1;
35
+ this.#save([{ id, title: trimmed, done: false }, ...this.all]);
36
+ }
37
+
38
+ toggle(id: number): void {
39
+ this.#save(this.all.map((todo) => (todo.id === id ? { ...todo, done: !todo.done } : todo)));
40
+ }
41
+
42
+ remove(id: number): void {
43
+ this.#save(this.all.filter((todo) => todo.id !== id));
44
+ }
45
+
46
+ #save(all: Todo[]): void {
47
+ this.all = all;
48
+ ApplicationSettings.setString(KEY, JSON.stringify(all));
49
+ }
50
+ }
51
+
52
+ function load(): Todo[] {
53
+ const saved = ApplicationSettings.getString(KEY);
54
+ return saved ? (JSON.parse(saved) as Todo[]) : FIRST;
55
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ // Native tags, their attributes and their events, for templates.
2
+ import 'ember-nativescript/glint';
@@ -0,0 +1,54 @@
1
+ // The app in Node, without a phone: its screens render into fake views (see ember-nativescript/testing), and
2
+ // ApplicationSettings is kept in memory. `app.page.toXML()` prints what a screen shows.
3
+ import { beforeEach, describe, expect, it } from 'vitest';
4
+ import { ApplicationSettings } from '@nativescript/core';
5
+ import { startApp } from 'ember-nativescript/testing';
6
+
7
+ import Home from '../src/components/Home.gts';
8
+ import Todos from '../src/services/todos.ts';
9
+
10
+ const start = () => startApp(Home, { services: { todos: Todos } });
11
+
12
+ describe('To-dos', () => {
13
+ beforeEach(() => ApplicationSettings.clear());
14
+
15
+ it('lists the first to-dos', async () => {
16
+ const app = await start();
17
+ expect(app.findAll('.todo-title').map((row) => row.text)).toEqual([
18
+ 'Open src/components/Home.gts',
19
+ 'Tap a to-do to open it',
20
+ 'Run the tests: pnpm test',
21
+ ]);
22
+ expect(app.find('#left').text).toBe('3 left');
23
+ });
24
+
25
+ it('adds a to-do on top, and keeps it', async () => {
26
+ const app = await start();
27
+ await app.type('#draft', 'Buy milk');
28
+ await app.tap('#add');
29
+
30
+ expect(app.findAll('.todo-title')[0]!.text).toBe('Buy milk');
31
+ expect(app.find('#draft').text).toBe('');
32
+ expect(app.find('#left').text).toBe('4 left');
33
+
34
+ const again = await start();
35
+ expect(again.findAll('.todo-title')[0]!.text).toBe('Buy milk');
36
+ });
37
+
38
+ it('opens a to-do, marks it done, and deletes it', async () => {
39
+ const app = await start();
40
+ await app.tapRow('#list', 1);
41
+ expect(app.find('#title').text).toBe('Tap a to-do to open it');
42
+
43
+ await app.tap('#toggle');
44
+ expect(app.find('#status').text).toBe('Done');
45
+
46
+ await app.back();
47
+ expect(app.find('#left').text).toBe('2 left');
48
+ expect(app.findAll('.done')).toHaveLength(1);
49
+
50
+ await app.tapRow('#list', 1);
51
+ await app.tap('#remove');
52
+ expect(app.findAll('.todo-title')).toHaveLength(2);
53
+ });
54
+ });
@@ -0,0 +1,6 @@
1
+ node_modules/
2
+ platforms/
3
+ hooks/
4
+ .ns-vite-build/
5
+ App_Resources/**/build/
6
+ coverage/
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "allowImportingTsExtensions": true,
7
+ "noEmit": true,
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "experimentalDecorators": true,
11
+ "types": ["ember-source/types"],
12
+ "lib": ["ES2023", "DOM"]
13
+ },
14
+ "include": ["src/**/*.ts", "src/**/*.gts", "tests/**/*.ts"]
15
+ }
@@ -0,0 +1,5 @@
1
+ // NativeScript's TypeScript config with .gts components, and Ember kept out of the HMR vendor bundle.
2
+ import { defineConfig } from 'vite';
3
+ import { emberConfig } from 'ember-nativescript/vite';
4
+
5
+ export default defineConfig(({ mode }) => emberConfig({ mode }));
@@ -0,0 +1,6 @@
1
+ // Tests run in Node: .gts components compile as in the app, and @nativescript/core is a stub (see
2
+ // ember-nativescript/testing).
3
+ import { defineConfig } from 'vitest/config';
4
+ import { emberTestConfig } from 'ember-nativescript/vitest';
5
+
6
+ export default defineConfig(emberTestConfig());